安卓的ActivityGroup - NullPointerException异常 [英] Android ActivityGroup - NullPointerException

查看:174
本文介绍了安卓的ActivityGroup - NullPointerException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用活动组 - 因为我用的标签,并希望有标签的列表项被点击加载和活动后的时候,。但我得到的NullPointerException以下行:

I'm trying to use activity groups - since I use tabs, and want to have the tabs when loading and activity after the list item was clicked,. but I'm getting nullpointerexception in the following line:

View view1 = S1_Group.group.getLocalActivityManager()
                           .startActivity("S1", intent)
                           .getDecorView();

code是...。

Code is .. .

lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                  int position, long id) {

Intent intent = new Intent(getApplicationContext(), S1.class);

                       intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                       Log.d("test","Before view");
                       try{
                           View view1 = S1_Group.group.getLocalActivityManager()
                           .startActivity("S1", intent)
                           .getDecorView();
                           Settings_Group.group.setContentView(view1);      
                       }
                       catch (Exception e){
                           Log.e("test","view failded:"+e);
                       }
....

感谢您的帮助!

Thanks for you help!!!

更新:这怎么我的团队活动是:我找不到什么问题, 公共类S1_Group扩展的ActivityGroup {

update: this how my group activity is: I couldn't find what was the issue., public class S1_Group extends ActivityGroup {

public static S1_Group group;
private ArrayList<View> history;



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    this.history = new ArrayList<View>();
    group = this;

    View view = getLocalActivityManager().startActivity("F1", 
            new Intent(this, F1.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();

    setContentView(view);
}

}

推荐答案

遇到了同样的问题,似乎有(或曾经)与当你试图发生的LocalActivityManager错误(重新)启动具有相同ID为$ P $活动pviously破坏活动。它会简单地返回,因为一个错误的destroyActivity方法内。我使用的解决方法,使用反射妥善销毁活动(该变通办法之后详细的说明):

Came across the same issue and it seems that there is(or was) a bug with the LocalActivityManager that occurs when you're trying to (re)start an Activity with the same ID as a previously destroyed Activity. It will simply return null as Window, because of a bug inside the destroyActivity method. The workaround I'm using, uses reflection to properly destroy a activity (detailed explanation after the workaround) :

public boolean destroyActivityWorkAround(String id) {
    final LocalActivityManager activityManager = getLocalActivityManager();
    if(activityManager != null){
        activityManager.destroyActivity(id, false);             
        try {
            // Use reflection to get to the HashMaps with the records(which activities are started ect.)
            // to remove the records properly 
            // http://code.google.com/p/android/issues/detail?id=10083
            final Field mActivitiesField = LocalActivityManager.class.getDeclaredField("mActivities");
            if(mActivitiesField != null){
                mActivitiesField.setAccessible(true);
                @SuppressWarnings("unchecked")
                final Map<String, Object> mActivities = (Map<String, Object>)mActivitiesField.get(activityManager);
                if(mActivities != null){
                    mActivities.remove(id);
                }
                final Field mActivityArrayField = LocalActivityManager.class.getDeclaredField("mActivityArray");
                if(mActivityArrayField != null){
                    mActivityArrayField.setAccessible(true);
                    @SuppressWarnings("unchecked")
                    final ArrayList<Object> mActivityArray = (ArrayList<Object>)mActivityArrayField.get(activityManager);
                    if(mActivityArray != null){
                        for(Object record : mActivityArray){
                            final Field idField = record.getClass().getDeclaredField("id");
                            if(idField != null){
                                idField.setAccessible(true);
                                final String _id = (String)idField.get(record);
                                if(id.equals(_id)){
                                    mActivityArray.remove(record);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            Log.e(LOGTAG, this.getClass().getSimpleName() + ".destroyActivityWorkAround() removing activity using reflection failed with error:", e);
            //e.printStackTrace();
        }
        return true;
    }
    return false;
}

这是一种解决方法,因为LocalActivityManager.destroyActivity(...)包含几个API-版本的bug。该方法不正确地从它的HashMap的(<一个删除活动href="http://www.netmite.com/android/mydroid/frameworks/base/core/java/android/app/LocalActivityManager.java"相对=nofollow> LocalActivityManager的源):

This is a workaround because the LocalActivityManager.destroyActivity(...) contains a bug in several API-versions. The method doesn't remove the Activity properly from its HashMap's (LocalActivityManager's source):

     public Window destroyActivity(String id, boolean finish) {
         LocalActivityRecord r = mActivities.get(id);   //<-- id's are the key's for the HashMap
         Window win = null;
         if (r != null) {
             win = performDestroy(r, finish);
             if (finish) {
                    mActivities.remove(r);  //--> This works on id's not the 'r object', this doesn't remove anything
             }
         }
         return win;
     } 

的,如果(完成)语句应尽可能遵循删除活动的LocalActivityRecord被破坏:

the if(finish) statement should be as followed to remove the LocalActivityRecord of the activity being destroyed:

if (finish) {
    mActivities.remove(id);    //--> mActivities should remove the id
    mActivityArray.remove(r);  //--> mActivitiesArray should remove the 'r object' (LocalActivityRecord)
}

虽然他们说这是被固定为Froyo的,但我还是遇到了它在三星Galaxy S2运行2.3.3

Although they say it's being fixed for Froyo but I still encountered it on a Samsung galaxy S2 running 2.3.3

这篇关于安卓的ActivityGroup - NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆