我遇到了 android 片段的问题。
如果我通过 Intent 调用了 Actitiy A 并且之前启动了 Actitiy,它就可以工作。在另一种情况下,如果我通过 Intent 调用了 Actitiy A 并且之前没有启动过 Actitiy,如果我尝试获取 Fragment 对象,将会抛出 NullPointerException。
我必须访问 Fragment,但如果 Activity 没有运行,我会得到 NPE。
活动中截取的代码:
public class HomeActivity extends BaseFragmentActivity {
@Override
protected void onNewIntent(Intent intent) {
if (intent.hasExtra("tab")) {
int tab = intent.getIntExtra("tab", 0);
mTabsHelper.setTab(tab);
if (intent.hasExtra("id")) {
String tickerName = intent.getStringExtra("id");
switchToExistTicker(tickerName);
}
}
}
private TickerListFragment getTickerListFragment() {
TickerListFragment tickerListFragment = (TickerListFragment) getSupportFragmentManager()
.findFragmentByTag("android:switcher:2131099811:1");
return tickerListFragment;
}
private void switchToNewTicker(String name) {
getTickerListFragment().setNewTicketName(name);
}
private void switchToExistTicker(String name) {
getTickerListFragment().switchToTicker(getTickerListFragment().getTickerIdForTickerName(name), true);
}
}
在另一个活动中,即通过通知发送 Intent 的响应,以下代码被剪断:
protected void sendnotification (String title, String message) {
Intent i = new Intent("android.intent.action.MAIN");
i.setComponent(new ComponentName("de.domain.android", "de.domain.android.HomeActivity"));
i.putExtra("tab", 1);
i.putExtra("id", "subject");
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, i, 0);
Notification n = new Notification(R.drawable.ic_launcher, message, System.currentTimeMillis());
n.flags = Notification.FLAG_AUTO_CANCEL;
n.setLatestEventInfo(getApplicationContext(), title, message, contentIntent);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(0, n);
}
发送通知后,用户触摸通知打开 Activity A。Activity A 收到带有某些附加功能的 Intent(有关更多详细信息,请参阅随附的代码片段)。如前所述,如果 Activity A 之前未启动,则 Activity 崩溃,因此它们必须重新启动。在这种情况下,找不到所需的 Fragment 并且“null”是返回值 - NullPointerException 是结果。同样如上所述,如果 Activity 之前启动,则 Activity 不会崩溃 - 然后它可以工作。
在异常库存之后:
8805 AndroidRuntime E FATAL EXCEPTION: main
8805 AndroidRuntime E java.lang.RuntimeException: Unable to start activity ComponentInfo{de.domain.android/de.domain.android.HomeActivity}: java.lang.NullPointerException
8805 AndroidRuntime E at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2202)
8805 AndroidRuntime E at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2237)
8805 AndroidRuntime E at android.app.ActivityThread.access$600(ActivityThread.java:139)
8805 AndroidRuntime E at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
8805 AndroidRuntime E at android.os.Handler.dispatchMessage(Handler.java:99)
8805 AndroidRuntime E at android.os.Looper.loop(Looper.java:154)
8805 AndroidRuntime E at android.app.ActivityThread.main(ActivityThread.java:4974)
8805 AndroidRuntime E at java.lang.reflect.Method.invokeNative(Native Method)
8805 AndroidRuntime E at java.lang.reflect.Method.invoke(Method.java:511)
8805 AndroidRuntime E at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
8805 AndroidRuntime E at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
8805 AndroidRuntime E at dalvik.system.NativeStart.main(Native Method)
8805 AndroidRuntime E Caused by: java.lang.NullPointerException
8805 AndroidRuntime E at de.domain.android.HomeActivity.switchToExistTicker(HomeActivity.java:144)
8805 AndroidRuntime E at de.domain.android.HomeActivity.onNewIntent(HomeActivity.java:127)
8805 AndroidRuntime E at de.domain.android.HomeActivity.onCreate(HomeActivity.java:94)
8805 AndroidRuntime E at android.app.Activity.performCreate(Activity.java:4538)
8805 AndroidRuntime E at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
8805 AndroidRuntime E at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2158)
8805 AndroidRuntime E ... 11 more
任何帮助和建议将不胜感激!
问候!