2017-09-24 157 views
-1

我在調試模式下運行應用程序,然後它不斷崩潰。在logicat監視它說:我的應用程序不斷崩潰,說「已停止」由:java.lang.NullPointerException

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

看起來像'imgBtn1'是'null' – Ramanlfc

+0

你是否在'imgButtonLoadPage'調用 –

+0

之前調用了'setContentView'如何解決這個問題?道歉我在java中仍然是新的 – user8605033

回答

0

這個問題可能是新來者最常問的問題,Android Studio標籤是無關緊要的。如果您在Java或Android中獲得NullPointerException,則可能未使用new關鍵字或findViewById()或相關方法實例化視圖或對象。簡而言之,您的視圖不會從xml文件中膨脹,並且在內存中有一個空指針。您應該在打開線程之前進行搜索,特別是NullPointerException線程。

相關問題