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());
}
}
}
我在线上遇到错误
Intent lastIntent = manager.getActivity(stack.peek()).getIntent();
错误为NullPointerException
。
那里可能有什么问题?
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)
这是堆栈跟踪!
答案 0 :(得分:2)
通过查看代码,我发现了一个可以创建NullPointerException的原因,即: 可能是您正在使用方法“public void push(String id,Intent intent)”与不同活动的相同id(String)。
创建空指针异常的原因是,当你调用具有相同id(String)的push方法并且Android mamory managemnt销毁与该名称相关联的旧活动时,在pop方法中它返回null。
希望这个答案有助于解决错误。
干杯, 的Viraj
答案 1 :(得分:1)
javadoc声明getActivity将返回
关联的Activity对象,如果id未知或其为,则返回null 活动目前尚未实例化
也许你的活动没有实例化,或者它已经被android破坏了。 然后你应该考虑启动它。
此致 斯特凡