我在调试模式下运行应用程序然后继续崩溃。在logicat监视器上,它说:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at nws.iceapp.MainActivity.imgButtonLoadPage(MainActivity.java:174)
at nws.iceapp.MainActivity.onCreate(MainActivity.java:160)
这是我点击(MainActivity.java:174)
时弹出的内容protected void imgButtonLoadPage (int buttonID, final int activity) {
ImageButton imgBtn = (ImageButton) findViewById(buttonID);
imgBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
setContentView(activity);
}
});
特别是从imgButtonLoadpage开始,当我点击(MainActivity.java:160)时,弹出:
imgButtonLoadPage(R.id.asthma, R.layout.act_ast);
imgButtonLoadPage(R.id.vomiting, R.layout.act_vom);
imgButtonLoadPage(R.id.unconscious, R.layout.act_unc);
imgButtonLoadPage(R.id.fracture2, R.layout.act_fra);
imgButtonLoadPage(R.id.dislocate2, R.layout.act_dis);
imgButtonLoadPage(R.id.heart_attack2, R.layout.act_hea);
}
这是布局xml代码:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="nws.iceapp.MainActivity">
<ScrollView
android:id="@+id/scrollView2"
android:layout_width="368dp"
android:layout_height="518dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.506"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<ImageButton
android:id="@+id/asthma"
android:layout_width="match_parent"
android:layout_height="115dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_weight="1.16"
android:scaleType="centerCrop"
android:src="@drawable/asthma_icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.189"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.11" />
<TextView
android:id="@+id/ast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Asthma"
android:background="#333"
android:textColor="#fff"
android:textSize="21sp" />
<ImageButton
android:id="@+id/vomiting"
android:layout_width="match_parent"
android:layout_height="115dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_weight="0.47"
android:scaleType="centerCrop"
android:src="@drawable/vomit_icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.189"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.452" />
<TextView
android:id="@+id/vom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Vomiting"
android:background="#333"
android:textColor="#fff"
android:textSize="21sp" />
<ImageButton
android:id="@+id/unconscious"
android:layout_width="match_parent"
android:layout_height="115dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:src="@drawable/lost_conscious_icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.774"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.11" />
<TextView
android:id="@+id/unc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Unconscious"
android:background="#333"
android:textColor="#fff"
android:textSize="21sp" />
<ImageButton
android:id="@+id/fracture2"
android:layout_width="match_parent"
android:layout_height="115dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:src="@drawable/fracture_icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.189"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.794" />
<TextView
android:id="@+id/fra"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fracture"
android:gravity="center"
android:background="#333"
android:textColor="#fff"
android:textSize="21sp" />
<ImageButton
android:id="@+id/dislocate2"
android:layout_width="match_parent"
android:layout_height="115dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:src="@drawable/dislocate_icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.774"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.452" />
<TextView
android:id="@+id/dis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Dislocate"
android:gravity="center"
android:background="#333"
android:textColor="#fff"
android:textSize="21sp" />
<ImageButton
android:id="@+id/heart_attack2"
android:layout_width="match_parent"
android:layout_height="115dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:scaleType="centerCrop"
android:src="@drawable/heart_attack_icon"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.774"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.794" />
<TextView
android:id="@+id/hea"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Heart Attack"
android:background="#333"
android:textColor="#fff"
android:gravity="center"
android:textSize="21sp" />
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
这个问题可能是新来者提出的问题最多,Android Studio标签也无关紧要。如果您在Java或Android中获得NullPointerException,则可能无法使用new
关键字或findViewById()
或相关方法实例化视图或对象。简而言之,您的视图不会从xml文件中膨胀,并且在内存中有一个空指针。您应该在打开线程之前进行搜索,尤其是NullPointerException线程。