0
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
    Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
}

Null Pointer Exception当我再次重新选择操作栏时,我的应用程序关闭并抛出一个。错误似乎发生在上述方法中。

日志猫:

> 03-19 20:26:54.075: E/AndroidRuntime(2506): FATAL EXCEPTION: main
> 03-19 20:26:54.075: E/AndroidRuntime(2506):
> java.lang.NullPointerException 03-19 20:26:54.075:
> E/AndroidRuntime(2506):   at android.widget.Toast.<init>(Toast.java:85)
> 03-19 20:26:54.075: E/AndroidRuntime(2506):   at
> android.widget.Toast.makeText(Toast.java:226) 03-19 20:26:54.075:
> E/AndroidRuntime(2506):   at
> com.example.MyTabsListener.onTabReselected(MainActivity.java:94)
> 03-19 20:26:54.075: E/AndroidRuntime(2506):   at
> com.android.internal.app.ActionBarImpl.selectTab(ActionBarImpl.java:453)
> 03-19 20:26:54.075: E/AndroidRuntime(2506):   at
> com.android.internal.app.ActionBarImpl$TabImpl.select(ActionBarImpl.java:787)
> 03-19 20:26:54.075: E/AndroidRuntime(2506):   at
> com.android.internal.widget.ActionBarView$TabClickListener.onClick(ActionBarView.java:950)
> 03-19 20:26:54.075: E/AndroidRuntime(2506):   at
> android.view.View.performClick(View.java:3100) 03-19 20:26:54.075:
> E/AndroidRuntime(2506):   at
> android.view.View$PerformClick.run(View.java:11644) 03-19
> 20:26:54.075: E/AndroidRuntime(2506):     at
> android.os.Handler.handleCallback(Handler.java:587) 03-19
> 20:26:54.075: E/AndroidRuntime(2506):     at
> android.os.Handler.dispatchMessage(Handler.java:92) 03-19
> 20:26:54.075: E/AndroidRuntime(2506):     at
> android.os.Looper.loop(Looper.java:126) 03-19 20:26:54.075:
> E/AndroidRuntime(2506):   at
> android.app.ActivityThread.main(ActivityThread.java:3997) 03-19
> 20:26:54.075: E/AndroidRuntime(2506):     at
> java.lang.reflect.Method.invokeNative(Native Method) 03-19
> 20:26:54.075: E/AndroidRuntime(2506):     at
> java.lang.reflect.Method.invoke(Method.java:491) 03-19 20:26:54.075:
> E/AndroidRuntime(2506):   at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
> 03-19 20:26:54.075: E/AndroidRuntime(2506):   at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 03-19
> 20:26:54.075: E/AndroidRuntime(2506):     at
> dalvik.system.NativeStart.main(Native Method) 03-19 20:31:54.545:
> I/Process(2506): Sending signal. PID: 2506 SIG: 9
4

1 回答 1

0

代替

Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();

Toast.makeText(getActivity(), "Reselected!", Toast.LENGTH_LONG).show();
于 2013-03-19T15:06:05.537 回答