我需要在大学使用 android studio,直到现在一切正常,但现在我无法在我的活动中显示按钮等内容。我尝试重新启动并使缓存无效,但它在模拟器或我的物理手机上没有显示任何内容。
那是第一个活动的 xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="158dp"
android:layout_marginTop="185dp"
android:text="Button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
那是第一个活动的代码
package com.example.blatt4.Aufgabe1
import android.content.Intent
import android.os.Bundle
import android.os.PersistableBundle
import android.util.Log
import android.view.MotionEvent
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import com.example.blatt4.MainActivity
import com.example.blatt4.R
class FirstActivity:AppCompatActivity() {
val TAG = "FirstActivity"
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
setContentView(R.layout.activity_first)
val button = findViewById<Button>(R.id.button2)
button.setOnClickListener{
startActivity(Intent(this, MainActivity::class.java))
}
}
override fun onTouchEvent(event: MotionEvent?): Boolean {
when (event!!.actionMasked) {
MotionEvent.ACTION_DOWN -> Log.d(TAG, "Touch event started at x = " + event.x + ", y = " + event.y)
MotionEvent.ACTION_UP -> Log.d(TAG, "User lifted their finger at x = " + event.x + ", y = " + event.y);
MotionEvent.ACTION_MOVE -> Log.d(TAG, "User is moving their finger at x = " + event.x + ", y = " + event.y)
MotionEvent.ACTION_CANCEL -> Log.d(TAG, "User canceled the touch event at x = " + event.x + ", y = " + event.y);
MotionEvent.ACTION_BUTTON_PRESS -> Log.d(TAG, "Button pressed at x = " + event.x + ", y = " + event.y);
//Multi Touch
/**
MotionEvent.ACTION_POINTER_DOWN -> Log.d(TAG, "Pointer Down" + event.x + ", y=" + event.y);
MotionEvent.ACTION_POINTER_UP -> Log.d(TAG, "Pointer Up" + event.x + ", y=" + event.y);
*/
}
return true
}
}
在我的主要 Activity 中,它工作正常,但之后没有任何显示。
package com.example.blatt4
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import com.example.blatt4.Aufgabe1.FirstActivity
import com.example.blatt4.Aufgabe2.SecondActivity
import com.example.blatt4.Aufgabe3.ThirdActivity
import com.example.blatt4.Aufgabe4.FourthActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val aufgabe1 = findViewById<Button>(R.id.aufgabe1)
val aufgabe2 = findViewById<Button>(R.id.aufgabe2)
val aufgabe3 = findViewById<Button>(R.id.aufgabe3)
val aufgabe4 = findViewById<Button>(R.id.aufgabe4)
aufgabe1.setOnClickListener {
startActivity(Intent(this, FirstActivity::class.java))
}
aufgabe2.setOnClickListener {
startActivity(Intent(this, SecondActivity::class.java))
}
aufgabe3.setOnClickListener {
startActivity(Intent(this, ThirdActivity::class.java))
}
aufgabe4.setOnClickListener {
startActivity(Intent(this, FourthActivity::class.java))
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/aufgabe1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="150dp"
android:layout_marginTop="170dp"
android:text="Aufgabe1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/aufgabe2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="150dp"
android:layout_marginTop="53dp"
android:text="Aufgabe2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/aufgabe1" />
<Button
android:id="@+id/aufgabe3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="150dp"
android:layout_marginTop="53dp"
android:text="Aufgabe3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/aufgabe2" />
<Button
android:id="@+id/aufgabe4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="150dp"
android:layout_marginTop="53dp"
android:text="Aufgabe4"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/aufgabe3" />
</androidx.constraintlayout.widget.ConstraintLayout>
我尝试使缓存无效,同步 gradle,但在主活动之后我没有在屏幕上显示任何内容。