我想從MainActivity.java
呼叫片段法的活動越來越NPE
這裏調用MainFragment.java
方法我都試過:
MainActivity.java
MainFragment mainFragment;
........
mainFragment = new MainFragment();
.........
mainFragment.deom();
MainFragment。 java
public void deom() {
Toast.makeText(getActivity(), "Demo", Toast.LENGTH_LONG).show();
}
登錄
java.lang.NullPointerException
at android.widget.Toast.<init>(Toast.java:93)
at android.widget.Toast.makeText(Toast.java:241)
at com.example.x.b(Unknown Source)
at com.example.MainActivity.onOptionsItemSelected(Unknown Source)
at android.app.Activity.onMenuItemSelected(Activity.java:2633)
at android.support.v4.b.ao.onMenuItemSelected(Unknown Source)
at android.support.v7.app.t.onMenuItemSelected(Unknown Source)
at android.support.v7.view.n.onMenuItemSelected(Unknown Source)
at android.support.v7.view.n.onMenuItemSelected(Unknown Source)
我可以知道我在做錯誤?我錯過了什麼?爲什麼我得到NPE。
你創建的'比如MainFragment'第一... – ByeBye
@Selvin這不是重複一個 – Sophie
@ByeBye試過相同,檢查更新的代碼及以上的日誌,仍然得到同樣的問題... – Sophie