2015-08-08 61 views
-1

所以我試圖將一個片段中的文本傳遞給主Activity,並將主Activity傳遞給另一個片段,以便顯示它。爲什麼我會在Android Studio(碎片)中獲取NullPointerException?

這裏是片段的代碼,將其發送給主要活動:

public class TopFragment extends Fragment{ 

private static EditText topInput; 
private static EditText bottomInput; 

FragmentInterface communicate; 

public interface FragmentInterface{ 
    public void sendTheInput(String topText,String bottomText); 
} 

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

    topInput = (EditText) view.findViewById(R.id.topInput); 
    bottomInput = (EditText) view.findViewById(R.id.bottomInput); 
    final Button submitButton = (Button)view.findViewById(R.id.submitButton); 

    submitButton.setOnClickListener(
      new View.OnClickListener() { 
       public void onClick(View v) { 
        buttonClicked(v); 
       } 
      } 
    ); 
    return view; 
} 

@Override 
public void onAttach(Activity activity) { 
    super.onAttach(activity); 
    try{ 
     communicate = (FragmentInterface) activity; 
    } catch (ClassCastException e){ 
     throw new ClassCastException(activity.toString()); 
    } 
} 

public void buttonClicked(View view){ 

    communicate.sendTheInput(topInput.getText().toString(),bottomInput.getText().toString()); 

} 

} 

這裏是主要的活動代碼:

@Override 
    public void sendTheInput(String topText, String bottomText) { 
     BottomFragment bottomFrag = (BottomFragment)getSupportFragmentManager().findFragmentById(R.id.bottomFragment); 
     bottomFrag.setBoxText(topText,bottomText); 
    } 

這裏是底部片段代碼:

public class BottomFragment extends Fragment{ 

private static EditText topTextBox; 
private static EditText bottomTextBox; 

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



    return view; 
} 

public void setBoxText(String topText, String bottomText){ 

    topTextBox.setText(topText); 
    bottomTextBox.setText(bottomText); 

} 
} 

當我運行應用程序,我沒有得到任何運行時錯誤,但我確實得到這個:

08-08 13:09:45.820 2241-2241/com.example.vanessaanthony.fragmentreview E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.vanessaanthony.fragmentreview, PID: 2241 
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference 
      at com.example.vanessaanthony.fragmentreview.BottomFragment.setBoxText(BottomFragment.java:29) 
      at com.example.vanessaanthony.fragmentreview.MainActivity.sendTheInput(MainActivity.java:20) 
      at com.example.vanessaanthony.fragmentreview.TopFragment.buttonClicked(TopFragment.java:55) 
      at com.example.vanessaanthony.fragmentreview.TopFragment$1.onClick(TopFragment.java:36) 
      at android.view.View.performClick(View.java:4780) 
      at android.view.View$PerformClick.run(View.java:19866) 
      at android.os.Handler.handleCallback(Handler.java:739) 
      at android.os.Handler.dispatchMessage(Handler.java:95) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5257) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 08-08 13:09:56.646 2241-2248/com.example.vanessaanthony.fragmentreview W/art﹕ Suspending all threads took: 5.142ms 

我是Android Studio的初學者,我不知道爲什麼我的程序無法工作。任何答案非常感謝。

回答

1

您在BottomFragment聲明兩個EditText S(實際上,你應該刪除static那裏,每個片段都有自己的編輯文本,但是這不是問題):

private static EditText topTextBox; 
private static EditText bottomTextBox; 

,但你不不給他們一個價值。 Android本身並不知道你的意思是哪個EditText;你必須找到他們使用findViewById。這通常是在onCreateView完成(用於碎片):

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.bottom_fragment,container,false); 
    topTextBox = (EditText)view.FindViewById(R.id.topTextBox); 
    bottomTextBox = ... 

就像你在TopFragment一樣。

+0

謝謝!不知道我錯過了這個。 –

相關問題