2014-01-06 34 views
-1

我將一個Button變量的值賦值給在我的XML佈局中創建的按鈕的一個,我已經給出了XML中的按鈕適合編號,我指的是這個值時,分配給保持爲空的Button。Android爲什麼這個Button在爲它的值賦值時保留爲null findViewById

的問題發生在這個方法:

public void addListenerOnButtons() { 


     updateInfoButton = (Button) findViewById(R.id.updateWalkInfoButton); 

     updateInfoButton.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       Intent resultIntent = new Intent(); 
       resultIntent.putExtra("newName", ""); 
       resultIntent.putExtra("newSDesc", ""); 
       resultIntent.putExtra("newLDesc", ""); 
       setResult(RESULT_OK, resultIntent); 
       finish(); 

      } 

     }); 


    helpButton = (ImageButton) findViewById(R.id.helpButton); 

    helpButton.setOnClickListener(new OnClickListener(){ 

     @Override 
     public void onClick(View v){ 

      Intent intent = new Intent(context, HelpScreen.class); 
      startActivity(intent); 
     } 
    }); 

} 

updateInfoButton保持爲空後,這種方法,這會導致我的應用程序崩潰,該頁面的XML如下:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/createScrollView" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@color/black" 
    tools:context=".EditWalksInfoActivity" > 

    <RelativeLayout 
     android:id="@+id/formLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <TextView 
      android:id="@+id/editWalkInfoHeaderTextView" 
      style="@android:style/TextAppearance.Large" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="15dp" 
      android:text="@string/editWalkInfoHeaderText" 
      android:textColor="@color/light_gray" /> 

     <EditText 
      android:id="@+id/editWalkNameEditText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/editWalkTitleTextView" 
      android:layout_marginTop="15dp" 
      android:ems="6" 
      android:hint="@string/createWalkNameHint" 
      android:inputType="textPersonName" 
      android:textColor="@color/light_gray" > 

      <requestFocus /> 
     </EditText> 

     <EditText 
      android:id="@+id/editWalkShortDescEditText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/editWalkShortDescTextView" 
      android:layout_marginTop="15dp" 
      android:ems="6" 
      android:hint="@string/createWalkShortDescHint" 
      android:inputType="text" 
      android:textColor="@color/light_gray" > 

      <requestFocus /> 
     </EditText> 

     <EditText 
      android:id="@+id/editWalkLongDescEditText" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/editWalkLongDescTextView" 
      android:layout_marginTop="15dp" 
      android:ems="6" 
      android:hint="@string/createWalkLongDescHint" 
      android:inputType="textMultiLine" 
      android:textColor="@color/light_gray" > 

      <requestFocus /> 
     </EditText> 

     <Button 
      android:id="@+id/updateWalkInfoButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@+id/editWalkLongDescEditText" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="15dp" 
      android:text="@string/updateWalkInfoButtonText" 
      android:textColor="@color/light_gray" /> 

     <TextView 
      android:id="@+id/editWalkShortDescTextView" 
      style="@android:style/TextAppearance.Medium" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_below="@+id/editWalkNameEditText" 
      android:layout_marginLeft="18dp" 
      android:layout_marginTop="15dp" 
      android:text="@string/createWalkShortDescTextView" 
      android:textColor="@color/light_gray" /> 

     <TextView 
      android:id="@+id/editWalkTitleTextView" 
      style="@android:style/TextAppearance.Medium" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/editWalkShortDescTextView" 
      android:layout_below="@+id/editWalkInfoHeaderTextView" 
      android:layout_marginTop="15dp" 
      android:text="@string/createWalkTitleTextView" 
      android:textColor="@color/light_gray" /> 

     <TextView 
      android:id="@+id/editWalkLongDescTextView" 
      style="@android:style/TextAppearance.Medium" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/editWalkShortDescTextView" 
      android:layout_below="@+id/editWalkShortDescEditText" 
      android:layout_marginTop="15dp" 
      android:text="@string/createWalkLongDescTextView" 
      android:textColor="@color/light_gray" /> 

     <ImageButton 
      android:id="@+id/editWalkHelpButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignLeft="@+id/editWalkTitleTextView" 
      android:layout_alignTop="@+id/editWalkInfoHeaderTextView" 
      android:background="@color/black" 
      android:contentDescription="@string/helpIconAltText" 
      android:src="@drawable/help" /> 
    </RelativeLayout> 

</ScrollView> 

清單。 XML

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="xxxxxx" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="22" /> 

    <permission 
     android:name="xxxxxx".MAPS_RECEIVE" 
     android:protectionLevel="signature" /> 

    <!-- Accessing camera hardware --> 
    <!-- putting android.hardware.camera prevents non-camera devices using this app --> 
    <uses-feature android:name="android.hardware.camera" /> 
    <uses-feature 
     android:glEsVersion="0x00020000" 
     android:required="true" /> 

    <uses-permission android:name="xxxxxx".MAPS_RECEIVE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:logo="@drawable/ic_launcher" 
     android:theme="@style/AppTheme" > 
     <uses-library 
      android:name="com.google.android.maps" 
      android:required="true" /> 

     <activity 
      android:name="xxxxxx.StartScreen" 
      android:label="@string/app_name" 
      android:screenOrientation="landscape" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="xxxxxx.CreateWalkActivity" 
      android:label="@string/title_activity_create_walk" 
      android:screenOrientation="landscape" > 
     </activity> 
     <activity 
      android:name="xxxxxx.HelpScreen" 
      android:label="@string/title_activity_help_screen" 
      android:screenOrientation="landscape" > 
     </activity> 
     <activity 
      android:name="xxxxxx.WalkRecording" 
      android:label="@string/title_activity_walk_recording" 
      android:screenOrientation="landscape" > 
     </activity> 
     <activity 
      android:name="xxxxxx.CreateNewPOIActivity" 
      android:label="@string/title_activity_create_new_poi" 
      android:screenOrientation="landscape" > 
     </activity> 

     <meta-data 
      android:name="com.google.android.maps.v2.API_KEY" 
      android:value="AIzaSyBriHozWKei0lNm0XP2SR3hz8GW790L1eA" /> 
     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 

     <activity 
      android:name="xxxxxx.ConfrimUploadActivity" 
      android:label="@string/title_activity_confrim_upload" 
      android:screenOrientation="landscape" > 
     </activity> 
     <activity 
      android:name="xxxxxx.ServerResponse" 
      android:label="@string/title_activity_server_response" 
      android:screenOrientation="landscape" > 
     </activity> 
     <activity 
      android:name="xxxxxx.EditWalksInfoActivity" 
      android:label="@string/title_activity_edit_walks_info" > 
     </activity> 
    </application> 

</manifest> 

這是logcat的輸出:

01-06 19:09:27.640: E/AndroidRuntime(6958): FATAL EXCEPTION: main 
01-06 19:09:27.640: E/AndroidRuntime(6958): Process: dcs.aber.ac.uk.cs211.group02, PID: 6958 
01-06 19:09:27.640: E/AndroidRuntime(6958): java.lang.RuntimeException: Unable to start activity ComponentInfo{dcs.aber.ac.uk.cs211.group02/dcs.aber.ac.uk.cs211.group02.EditWalksInfoActivity}: java.lang.NullPointerException 
01-06 19:09:27.640: E/AndroidRuntime(6958):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
01-06 19:09:27.640: E/AndroidRuntime(6958):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
01-06 19:09:27.640: E/AndroidRuntime(6958):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
01-06 19:09:27.640: E/AndroidRuntime(6958):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
01-06 19:09:27.640: E/AndroidRuntime(6958):  at android.os.Handler.dispatchMessage(Handler.java:102) 
01-06 19:09:27.640: E/AndroidRuntime(6958):  at android.os.Looper.loop(Looper.java:136) 
01-06 19:09:27.640: E/AndroidRuntime(6958):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
01-06 19:09:27.640: E/AndroidRuntime(6958):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-06 19:09:27.640: E/AndroidRuntime(6958):  at java.lang.reflect.Method.invoke(Method.java:515) 
01-06 19:09:27.640: E/AndroidRuntime(6958):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
01-06 19:09:27.640: E/AndroidRuntime(6958):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
01-06 19:09:27.640: E/AndroidRuntime(6958):  at dalvik.system.NativeStart.main(Native Method) 
01-06 19:09:27.640: E/AndroidRuntime(6958): Caused by: java.lang.NullPointerException 
01-06 19:09:27.640: E/AndroidRuntime(6958):  at dcs.aber.ac.uk.cs211.group02.EditWalksInfoActivity.addListenerOnButtons(EditWalksInfoActivity.java:78) 
01-06 19:09:27.640: E/AndroidRuntime(6958):  at dcs.aber.ac.uk.cs211.group02.EditWalksInfoActivity.onCreate(EditWalksInfoActivity.java:30) 
01-06 19:09:27.640: E/AndroidRuntime(6958):  at android.app.Activity.performCreate(Activity.java:5231) 
01-06 19:09:27.640: E/AndroidRuntime(6958):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
01-06 19:09:27.640: E/AndroidRuntime(6958):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
01-06 19:09:27.640: E/AndroidRuntime(6958):  ... 11 more 

編輯:

順序方法叫做:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_edit_walks_info); 
     context = this; 
     locateUIElements(); 
     addListenerOnButtons(); 
     Bundle bundle = getIntent().getBundleExtra("Walk info"); 
     if (bundle != null){ 

      name = bundle.getString("walkTitle"); 
      sDesc = bundle.getString("walkSDesc"); 
      lDesc = bundle.getString("walkLDesc"); 
      setEditTextValues(); 

     } 

    } 
+2

這很奇怪,我沒有看到任何錯誤,也許你忘了調用'setContentView'? – zozelfelfo

+0

什麼是第78行? –

+0

@RohanKandwal這是第78行:helpButton.setOnClickListener(new OnClickListener() –

回答

1

有一個潛在的災難,在這裏:

updateInfoButton =(按鈕)findViewById(R.id.updateWalkInfoButton );

機會,到時候該代碼被執行的視圖還沒有準備好是非常高的,並保持一個空引用,請務必移動執行的setContentView後某處代碼,也許在OnCreate的setContentView之後或在視圖後的某處設置...

關心!

+0

爲我的帖子添加了代碼,它實際上是按正確順序調用的。謝謝。 –

+0

如果將「addListenerOnButtons」方法移動到「onStart」,會發生什麼情況? –

+0

發生同樣的問題。 –

2

您可能試圖在活動視圖層次結構被充氣之前獲取按鈕。在Activity.setContentView之後調用addListenerOnButtons方法。

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // call sequence is important 
    setContentView(R.layout.your_layout); 
    addListenerOnButtons(); 
} 

希望這會有所幫助。

+0

它被調用後更新我的帖子。 –

0

是否設置了錯誤的上下文?是activity_edit_walks_info您發佈的ScrollView佈局XML的名稱?因爲在XML中定義的上下文是EditWalksInfoActivity

另外,是updateInfoButton在addListenerOnButtons()中設置全局嗎?如果是這樣,你可以發佈定義的地方嗎?

+0

按鈕updateInfoButton被定義爲包含addListenerOnButtons()方法的類的私有成員,它被定義爲私有按鈕updateInfoButton; –

4

問題在於你的XML中沒有id/helpButton

helpButton = (ImageButton) findViewById(R.id.helpButton); 

<ImageButton 
     android:id="@+id/editWalkHelpButton" 

R.id.helpButton可能是從其他一些資源文件,所以這是一個有效的標識符。

您看到的後續問題是因爲生成的R.java和二進制XML文件中的標識符不同步。編輯佈局XML文件和使用@+id標識符重新排序元素時,這是一個常見問題。解決方案是清理和重建項目。

+0

我更新了這個以反映XML,所以它現在讀取了editWalkHelpButton作爲helpButton的R.id,這並沒有幫助我的問題,這是updateInfoButton爲null的事實,我檢查了de bugger –

+0

如果您最近編輯佈局XML,清理並重建以防萬一。 R.java和二進制XML文件中的資源ID不同步是一個常見問題,給您帶來各種問題。 – laalto

+0

哇,認真!就是這一切!非常感謝!它一直在做我的頭腦! –

0

解決這個問題是簡單地清理項目:

在Eclipse中去:

項目>乾淨,它似乎解決了很多的問題,你可能有關於應用與其資源不同步。

特別感謝用戶:laalto的建議。

相關問題