I have got a problem with android fragments.
If I called the Actitiy A via Intent and the Actitiy is started before, it works.
In the other case, if I called the Actitiy A via Intent and the Actitiy is NOT started before, there will be throw a NullPointerException, if I try to get the Fragment Object.
I have to get access to the Fragment, but I got a NPE if the Activity is not running.
Code Snipped within Activity:
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);
}
}
In the other Activity, which is reponsibel for sending the Intent via Notification, the following Code Snipped:
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);
}
After the notification was send, the user touch the notification to open the Activity A. The Activity A receive the Intent with certain extras (for more detail, please see attached code snippets). As described the Activity crashed if the Activity A is not started before, so they have to start new. In this case, the needed Fragment could not find and “null” is the return value – NullPointerException is the result.
Also as described the Activity will not crash if the Activity started before – then it works.
Following the exception stock:
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
Any help and suggestions will be appreciated!
Regards!
Your NPE is logged with class na line, as stack trace tells:
since you did not provide that code you have to check your HomeActivity.java line 144 to see if you i.e. do not try to access objects that may under some condition be null. Anyway, plant a breakpoint there and debug. W/o related source I can’t give more detailed pointers.