简体   繁体   中英

android intent giving nullpointer exception

package com.org.myOxygen.activities;

import java.util.Stack;

import android.app.Activity;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;

public class ApplicationTabActivityGroup extends ActivityGroup{

private static Stack<String> stack;

 @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (stack == null) stack = new Stack<String>();

    //start default activity
    push("Search Activity", new Intent(this, ApplicaitonActivity.class));

  }

  @Override
  public void finishFromChild(Activity child) {

          pop();
  }

  @Override
  public void onBackPressed() 
  {

          pop();
  }


  public void push(String id, Intent intent) 
  {
    Window window = getLocalActivityManager().startActivity(id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    if (window != null) {
      stack.push(id);
      setContentView(window.getDecorView());
    }
  }

  public void pop() 
  {
    if (stack.size() == 1) finish();
    LocalActivityManager manager = getLocalActivityManager();

    manager.destroyActivity(stack.pop(), true);

    if (stack.size() > 0) 
    {
      Intent lastIntent = manager.getActivity(stack.peek()).getIntent();
      Window newWindow = manager.startActivity(stack.peek(), lastIntent);
      setContentView(newWindow.getDecorView());
    }
  }

}

I am getting error on line

Intent lastIntent = manager.getActivity(stack.peek()).getIntent();

The error is NullPointerException .

What might be wrong there?

edit - 1

  11-26 14:58:57.256: E/AndroidRuntime(557): FATAL EXCEPTION: main
11-26 14:58:57.256: E/AndroidRuntime(557): java.lang.NullPointerException
11-26 14:58:57.256: E/AndroidRuntime(557):  at com.org.myOxygen.activities.SearchTabActivityGroup.pop(SearchTabActivityGroup.java:59)
11-26 14:58:57.256: E/AndroidRuntime(557):  at com.org.myOxygen.activities.SearchTabActivityGroup.onBackPressed(SearchTabActivityGroup.java:36)
11-26 14:58:57.256: E/AndroidRuntime(557):  at android.app.Activity.onKeyUp(Activity.java:1888)
11-26 14:58:57.256: E/AndroidRuntime(557):  at android.view.KeyEvent.dispatch(KeyEvent.java:1061)
11-26 14:58:57.256: E/AndroidRuntime(557):  at android.app.Activity.dispatchKeyEvent(Activity.java:2068)
11-26 14:58:57.256: E/AndroidRuntime(557):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643)
11-26 14:58:57.256: E/AndroidRuntime(557):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
11-26 14:58:57.256: E/AndroidRuntime(557):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
11-26 14:58:57.256: E/AndroidRuntime(557):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
11-26 14:58:57.256: E/AndroidRuntime(557):  at android.widget.TabHost.dispatchKeyEvent(TabHost.java:275)
11-26 14:58:57.256: E/AndroidRuntime(557):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
11-26 14:58:57.256: E/AndroidRuntime(557):  at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:788)
11-26 14:58:57.256: E/AndroidRuntime(557):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1667)
11-26 14:58:57.256: E/AndroidRuntime(557):  at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1102)
11-26 14:58:57.256: E/AndroidRuntime(557):  at android.app.Activity.dispatchKeyEvent(Activity.java:2063)
11-26 14:58:57.256: E/AndroidRuntime(557):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643)
11-26 14:58:57.256: E/AndroidRuntime(557):  at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471)
11-26 14:58:57.256: E/AndroidRuntime(557):  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441)
11-26 14:58:57.256: E/AndroidRuntime(557):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1735)
11-26 14:58:57.256: E/AndroidRuntime(557):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-26 14:58:57.256: E/AndroidRuntime(557):  at android.os.Looper.loop(Looper.java:123)
11-26 14:58:57.256: E/AndroidRuntime(557):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-26 14:58:57.256: E/AndroidRuntime(557):  at java.lang.reflect.Method.invokeNative(Native Method)
11-26 14:58:57.256: E/AndroidRuntime(557):  at java.lang.reflect.Method.invoke(Method.java:521)
11-26 14:58:57.256: E/AndroidRuntime(557):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-26 14:58:57.256: E/AndroidRuntime(557):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-26 14:58:57.256: E/AndroidRuntime(557):  at dalvik.system.NativeStart.main(Native Method)

Here is the stack trace!

By looking at your code I have found one reason which can create the NullPointerException and that is, may be you are using method "public void push(String id, Intent intent)" with the same id(String) for diffrent activity.

reason why it creates null pointer exception is, when you call push method with same id(String) and Android mamory managemnt destroy the old activity associated with that name and during your pop method it returns null.

Hope this answer helps to solve bug.

Cheers, Viraj

The javadoc states that getActivity will return

the associated Activity object, or null if the id is unknown or its activity is not currently instantiated

Maybe you activity is not instanciated or it has been destroyed by android. You should then consider starting it.

Regards, Stéphane

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM