![](/img/trans.png)
[英]Android App keeps stopping when i try to add data from firebase database to a firebaseViewHolder
[英]android app keeps stopping when i try to open on emulator
我是android開發的初學者。 我創建了一個活動並添加了一個按鈕。 然后我添加了一個事件偵聽器以在單擊按鈕時打開另一個活動。 到目前為止,這兩個類是
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button floating_action_button = findViewById(R.id.fab);
floating_action_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, navActivity.class);
startActivity(intent);
}
});
}
}
和
public class navActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nav);
}
}
MainActivity的布局文件如下
<?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">
<TextView
android:layout_width="244dp"
android:layout_height="78dp"
android:text="Click on the red button to add the image you want to edit"
android:textFontWeight="600"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.484" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.954"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.915"
app:srcCompat="@android:drawable/ic_input_add"
tools:layout_margin="16dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
誰能告訴我這有什么問題嗎? 為什么應用程序一直停止?
我發現了我必須使用的錯誤
FloatingActionButton floating_action_button = findViewById(R.id.fab);
代替Button floating_action_button = findViewById(R.id.fab)
; 在偶數處理程序中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.