2013-07-09 74 views
1

對於我的活動,我有一個textview,我想要文本從GridView更改onItemClick。對於我的生活,我無法弄清楚爲什麼當我點擊gridview Item來更改textview的文本時,它爲什麼會一直創建nullPointerException。 「instrucitontextview」是有問題的文本視圖,我正在更改onItemClick底部的文本。TextView nullPointerException onItemClick - 內容視圖已設置

我有contentView設置,所以它應該能夠正確引用佈局。我已經在onCreate中對它進行了decalred,所以不應該引用問題。真的在這裏輸了。

Activity.java

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.jungle_activity); 

final TextView instructiontextview = (TextView) findViewById(R.id.textview_jungle_instructions); 

final GridView gridviewNeutrals = (GridView) findViewById(R.id.jungle_gridview_neutrals); 

gridviewNeutrals.setAdapter(new JungleImageAdapterNeutral(
      getApplicationContext())); 

gridviewNeutrals.setOnItemClickListener(new OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> parentView, View imageview, 
       int position, long id) { 
      if (position == 0) { 
       if (!JungleTimerService.baronJungleTimerStarted) { 
        Log.i(TAG, "baron jungle started"); 
        JungleTimerService.baronJungleTimerStarted = true; 
        textviewArrayListIndex(); 
        createBaronTimer(); 
        mServer.baronJungleTimer(); 
        TextView name = (TextView) imageview 
          .findViewById(R.id.jungle_grid_custom_text); 
        name.setTextSize(13); 
        FrameLayout.LayoutParams llp = new FrameLayout.LayoutParams(
          FrameLayout.LayoutParams.MATCH_PARENT, 
          FrameLayout.LayoutParams.MATCH_PARENT); 
        name.setLayoutParams(llp); 
        imageview 
          .setPadding(
            (int) getResources() 
              .getDimensionPixelSize(
                R.dimen.jungle_timer_button_pressed_dp), 
            (int) getResources() 
              .getDimensionPixelSize(
                R.dimen.jungle_timer_button_pressed_dp), 
            (int) getResources() 
              .getDimensionPixelSize(
                R.dimen.jungle_timer_button_pressed_dp), 
            (int) getResources() 
              .getDimensionPixelSize(
                R.dimen.jungle_timer_button_pressed_dp)); 
        instructiontextview.setText("worked"); 
       } 
      } 

logcat的

07-09 10:56:42.826: E/AndroidRuntime(7992): FATAL EXCEPTION: main 
07-09 10:56:42.826: E/AndroidRuntime(7992): java.lang.NullPointerException 
07-09 10:56:42.826: E/AndroidRuntime(7992):  at com.divinityworks.leaguecentral_leagueoflegends.JungleActivity$3.onItemClick(JungleActivity.java:164) 
07-09 10:56:42.826: E/AndroidRuntime(7992):  at android.widget.AdapterView.performItemClick(AdapterView.java:298) 
07-09 10:56:42.826: E/AndroidRuntime(7992):  at android.widget.AbsListView.performItemClick(AbsListView.java:1268) 
07-09 10:56:42.826: E/AndroidRuntime(7992):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:3059) 
07-09 10:56:42.826: E/AndroidRuntime(7992):  at android.widget.AbsListView$1.run(AbsListView.java:3950) 
07-09 10:56:42.826: E/AndroidRuntime(7992):  at android.os.Handler.handleCallback(Handler.java:615) 
07-09 10:56:42.826: E/AndroidRuntime(7992):  at android.os.Handler.dispatchMessage(Handler.java:92) 
07-09 10:56:42.826: E/AndroidRuntime(7992):  at android.os.Looper.loop(Looper.java:137) 
07-09 10:56:42.826: E/AndroidRuntime(7992):  at android.app.ActivityThread.main(ActivityThread.java:4950) 
07-09 10:56:42.826: E/AndroidRuntime(7992):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-09 10:56:42.826: E/AndroidRuntime(7992):  at java.lang.reflect.Method.invoke(Method.java:511) 
07-09 10:56:42.826: E/AndroidRuntime(7992):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 
07-09 10:56:42.826: E/AndroidRuntime(7992):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 
07-09 10:56:42.826: E/AndroidRuntime(7992):  at dalvik.system.NativeStart.main(Native Method) 

Jungle_activity.xml

<ScrollView 
    android:id="@+id/jungle_scrollview_timers" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="10" > 

    <FrameLayout 
     android:id="@+id/framelayout_jungle_scrollview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

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

     <TextView 
      android:id="@+id/jungle_textview_instructions" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="15dp" 
      android:gravity="center" 
      android:text="@string/textview_jungle_instructions" 
      android:textSize="25sp" /> 
    </FrameLayout> 
</ScrollView> 

謝謝小號

+0

顯示logcat的數據 – stinepike

+0

'TextView的名稱=(TextView的)ImageView的 .findViewById(R.id.jungle_grid_custom_text)'必須爲null檢查,如果名稱爲null – Raghunandan

+0

這行給出你是例外嗎? – Kostia

回答

2

你有這樣的

final TextView instructiontextview = 
(TextView) findViewById(R.id.textview_jungle_instructions); 

但是你有這樣的XML

<TextView 
     android:id="@+id/jungle_textview_instructions" 

你不必在你的XML ID textview_jungle_instructions一個TextView。

由於textview未初始化,您將獲得NullPointerException

所以應該

final TextView instructiontextview = 
(TextView) findViewById(R.id.jungle_textview_instructions); 
+0

感嘆,感謝您的幫助。這樣一個愚蠢的錯誤。假設這就是我爲命名文本字符串所得到的幾乎與textviews id相同的結果。 – Razgriz231

+0

@ Razgriz231歡迎您。很高興它有幫助。有時候會發生,但你會習慣於調試。 logcat是你應該看的地方。 – Raghunandan

相關問題