0
該問題與SO帖子中的this類似。獲取片段內的活動給出空指針異常
@Jason回答說 -
Your Fragment hasn't attached to the Activity yet, which also means your layout hasn't been inflated yet.
在這種情況下,這個問題是簡單地從一個片段傳遞到另一個字符串。
不過,在這裏我需要做不同的事情:
Common.java
public class Common extends Fragment
{
OnSelectedListener mCallback = new OnSelectedListener(){
public void getFailureDialog(){
RecordFailure fd = new RecordFailure();
fd.show(getActivity().getSupportFragmentManager(), "dialog"); //Null pointer exception here.
}
};
我叫mCallback.getFailureDialog()
調用該方法。
錯誤日誌:
03-22 18:21:30.022: E/AndroidRuntime(9385): java.lang.NullPointerException
03-22 18:21:30.022: E/AndroidRuntime(9385): at com.cornmain.util.Common$1.getFailureDialog(Common.java:112)
如何做到這一點?
UPDATE:
如果我使用fd.show(getFragmentManager(), "dialog");
,然後我得到了以下錯誤:
錯誤日誌:
03-22 18:35:18.432: E/AndroidRuntime(11872): java.lang.NullPointerException
03-22 18:35:18.432: E/AndroidRuntime(11872): at android.support.v4.app.DialogFragment.show(DialogFragment.java:125)
03-22 18:35:18.432: E/AndroidRuntime(11872): at com.cornmain.util.Common$1.getFailureDialog(Common.java:112)
你在哪裏調用getFailureDialog()?你怎麼聲明你的片段:在代碼或XML? – tundundun 2013-03-22 12:59:01
請看看那個:http://stackoverflow.com/questions/6215239/getactivity-returns-null-in-fragment-function 它的同樣的問題... – 2013-03-22 13:00:49
您是否使用具有最低的API級別8你正在使用'getSupportFragmentManager()'而不是'getFragmentManager()'? – GrIsHu 2013-03-22 13:03:12