2015-08-28 49 views
-1

我承認對Android開發和Java一般來說都很新穎,在我看到很多其他人也在搜索的東西中遇到了一些問題,但其他解決方案似乎並不像爲我做這件事。我有這個確切的代碼在另一個文件中工作,將它複製到另一個程序(從NavigationDrawer切換到NavigationView),現在Fragment在嘗試訪問TextView時給了我一個NullPointerException。從片段中訪問TextView中的佈局

就在我可以告訴我在定義視圖之前試圖撥打findViewById。我絕對肯定,問題出在方法show_life1_total()與線

TextView life1_total = (TextView) view.findViewById(R.id.life1_total); 

,但我無法找出什麼是錯的線,因爲它在不同的文件中完美地工作。非常感激任何的幫助。或者,如果還有其他方式,我應該在處理片段中的按鈕和視圖,請賜教。他們在活動中非常容易,但NavigationView迫使我使用片段,並且爲我打算在片段中使用的每個按鈕設置onClickListeners,看起來它將是一個巨大的應該是不必要的工作。也許我錯過了一些明顯的東西。

這裏是我的片段

package com.android4dev.navigationview; 

import android.app.Activity; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.TextView; 

/** 
* Created by Admin on 04-06-2015. 
*/ 
public class GameFragment extends Fragment implements View.OnClickListener{ 

    public static Integer nlife1Total=20; 
    public static Integer nlife2Total=20; 
    View view; 
    Button life1_minus; 
    Button life1_plus; 
    Button life2_minus; 
    Button life2_plus; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_life_counter,container,false); 

     life1_minus = (Button) view.findViewById(R.id.life1_minus); 
     life1_minus.setOnClickListener(this); 

     life1_plus = (Button) view.findViewById(R.id.life1_plus); 
     life1_plus.setOnClickListener(this); 

     life2_minus = (Button) view.findViewById(R.id.life2_minus); 
     life2_minus.setOnClickListener(this); 

     life2_plus = (Button) view.findViewById(R.id.life2_plus); 
     life2_plus.setOnClickListener(this); 

     show_life1_total(); 
     show_life2_total(); 

     return view; 
    } 

    public void startNewGame() 
    { 
     nlife1Total=20; 
     nlife2Total=20; 
     //show_life1_total(); 
     //show_life2_total(); 
    } 

    public void show_life1_total() { 
     TextView life1_total = (TextView) view.findViewById(R.id.life1_total); 
     String display_life1_total = String.valueOf(nlife1Total); 
     life1_total.setText(display_life1_total); 
    } 


    public void show_life2_total() { 
     TextView life2_total = (TextView) view.findViewById(R.id.life2_total); 
     String display_life2_total = String.valueOf(nlife2Total); 
     life2_total.setText(display_life2_total); 
    } 

    @Override 
    public void onClick(View v) { 
     switch(v.getId()){ 
      case R.id.life1_minus: 
       nlife1Total--; 
       show_life1_total(); 
       break; 
      case R.id.life1_plus: 
       nlife1Total++; 
       show_life1_total(); 
       break; 
      case R.id.life2_minus: 
       nlife2Total--; 
       show_life2_total(); 
       break; 
      case R.id.life2_plus: 
       nlife2Total++; 
       show_life2_total(); 
       break; 
     } 
    } 
} 

從XML文件中的相關觀點(這是fragment_life_counter.xml)

<TextView 
     android:id="@+id/life1_total" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:textSize="140sp" 
     android:text="@string/life1_total" 
     android:fontFamily="sans-serif-thin" 
     android:textColor="@color/life_color" 
     android:background="@drawable/backdrop_ubr_grixis" /> 

隨着

08-27 22:17:46.103 18943-18943/com.android4dev.navigationview E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.android4dev.navigationview, PID: 18943 
    java.lang.NullPointerException 
      at com.android4dev.navigationview.GameFragment.show_life1_total(GameFragment.java:69) 
      at com.android4dev.navigationview.GameFragment.onCreateView(GameFragment.java:54) 
      at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962) 
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1016) 
      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1197) 
      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 
      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1562) 
      at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:483) 
      at android.os.Handler.handleCallback(Handler.java:733) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:157) 
      at android.app.ActivityThread.main(ActivityThread.java:5293) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081) 
      at dalvik.system.NativeStart.main(Native Method) 

回答

0

你已經宣佈的logcat該方法中的視圖將充當局部變量,並將被銷燬。讓它成爲全局的,或者你已經聲明瞭一個全局的視圖varibale來初始化它。

按照這個 -

〜的相反

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fragment_life_counter,container,false); 

取出查看,因爲你已經宣佈它的類的頂部。

使它像this--

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    view = inflater.inflate(R.layout.fragment_life_counter,container,false); 

記得您試圖訪問未初始化的show_life1_total()方法的視圖對象。

我希望這會幫助你。

+0

完美,非常感謝!我知道我已經在全球宣佈它,所以show_life1_total()可以找到它,我錯過了在該行開頭的「視圖」在本地再次聲明它,而不是使用全局。感謝幫助! –

+0

現在接受。這讓我等了5分鐘才讓我接受。 –