Android 初始化的数组如何变为空?

Android 初始化的数组如何变为空?,android,nullpointerexception,android-activity,Android,Nullpointerexception,Android Activity,代码如下: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); overridePendingTransition(R.anim.fadein, R.anim.fadeout); setContentView(R.layout.startpage); rowItems = new ArrayList<RowItem>();

代码如下:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    setContentView(R.layout.startpage);

    rowItems = new ArrayList<RowItem>();

    //... Filling this array.
}
然后调用另一个活动:

public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_UP:
        int x = (int) event.getX();
        int y = (int) event.getY();
        int w=view.getWidth()-20;
        int h=view.getHeight()-20;
        if (x<w*0.05 || x>w*0.95 || y<h*0.13 ) return false; // Misclicked

        if (x<w*0.5 && y<h*0.38) {             
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }
}
    return true;
} 
public class MainActivity extends ListActivity implements View.OnClickListener {
void refresh_list() {
    if (StartPage.rowItems.size()>0) {  <-- Here is NPE
    ListAdapter adapter = new CustomListAdapter(MainActivity.this,R.layout.list_item,StartPage.rowItems); 
    setListAdapter(adapter);
    ((BaseAdapter) getListAdapter()).notifyDataSetChanged();
    }
}
希望这有助于…

如果我没弄错的话: 启动活动时,将调用onCreate()方法。 但是当您从另一个活动返回到同一个活动时,将跳过onCreate方法并调用onResume()方法。因此,我的建议是在onResume()方法中进行初始化

@覆盖
恢复时受保护的void(Bundle savedInstanceState){
覆盖转换(R.anim.fadein,R.anim.fadeout);
setContentView(R.layout.startpage);
rowItems=新的ArrayList();
//…填充这个数组。
}

您正在将数据加载到静态ArrayList中,并将其访问到不同的活动。这不是一个好的做法

首先让我告诉你答案,因为你已经在Oncreate()中创建了这个对象。最好是在全局范围内创建,否则此问题将不会发生

例如:

public class StartPage extends Activity implements View.OnTouchListener {

public static List<RowItem> rowItems = new ArrayList<RowItem>(); 

OnCreate(){
   //and perform the action you want to do.  

}
//Hope this will help you definately.
要传递数组对象,必须使用intent PUTTERA,例如:

Intent intent = new Intent(SplashScreen.this, MainActivity.class);

intent.putExtra("key",array); startActivity(intent);

//intent.putExtra("key",array); will show error if your Model class is not  implements Serializable  eg: public class Model  implements Serializable{

String id;
String price;
String name;
//generate your getter setter and set data in to this.
}

//For getting data in to another class just use

ArrayList<Model> data = (ArrayList<Model>)getIntent().getSerializable("key");
Intent Intent=新的Intent(SplashScreen.this,MainActivity.class);
intent.putExtra(“键”,数组);星触觉(意向);
//intent.putExtra(“键”,数组);如果您的模型类不可实现序列化,则将显示错误,例如:公共类模型可实现序列化{
字符串id;
串价;
字符串名;
//生成getter setter,并将数据设置到其中。
}
//要将数据导入另一个类,只需使用
ArrayList数据=(ArrayList)getIntent().getSerializable(“键”);
现在您可以使用此数据对象玩arround。您应该始终尝试使用私有或受保护的对象。
希望这会对您有所帮助。

这个答案可能无法解决您当前的问题(没有足够的代码提供建议),但会帮助您朝着正确的方向前进

请为您的对象提供一个中央数据存储,您应该考虑使用单点设计模式。此外,由于数据将从多个线程访问,因此您应该使arraylist(在您的示例中)是线程安全的


注意:如果您使用的是synchronized list,则应该锁定对象,以防止在迭代对象时进行访问

仅凭你提供的数据很难判断。你需要在这个
ArrayList
中包含任何相关的代码,并指出哪一行恰好抛出了
NPE
。也许StartPage是空的,就像你说的那样。这就解释了。“父活动(包含整个应用程序的所有全局变量)”如果我理解正确,这是一个糟糕的设计。在一个活动中保留资源,即使它不再出现在屏幕上,也会导致内存泄漏。KAYSER,这个起始页刚刚称为MainActivity。它不应该为null,除非android从内存中卸载它。这就是问题所在——是否可能(android从前台应用卸载非前台活动?),如果可能,如何防止?neeraj2608没有找到更好的方法将参数传递给内部函数。此外,StartPage的作用类似于定时更新,并且每次都会检索数据,因此其他活动(当前处于活动状态的活动)可以使用更新的数据…好的,对此问题进行了一些调查。看起来框架确实从内存中删除了父活动(如果它需要内存),并且假设如果用户想要返回到它,就调用onCreate。但是,到目前为止,任何对StartPage的调用都会导致NPE。如果我输入MainActivity.onCreate行,比如List items=StartPage.rowItems;将确保此阵列具有来自前端活动的指针,并保证android不会接触此阵列?最近只使用本地指针访问它?这不一定是真的。有时可能会发生这种情况,但当您完成顶部的
活动时,可能是
Android操作系统
杀死了调用方
活动的前一个实例
,因为它需要释放内存,因此将调用
onCreate()
。这就是为什么在onResume()中初始化它是安全的方法cuz即使该活动是第一次创建的…它仍然会在onCreate()之后调用onResume()方法,但是当您从另一个活动返回时,onCreate()方法不会被调用。我看不出其中的逻辑。如果活动已销毁,则将调用onCreate。如果不是,则初始化的变量不应为null,因此无需再次初始化,也没有问题。。。
@Override
protected void onResume(Bundle savedInstanceState) {
    overridePendingTransition(R.anim.fadein, R.anim.fadeout);
    setContentView(R.layout.startpage);

    rowItems = new ArrayList<RowItem>();

    //... Filling this array.
}
public class StartPage extends Activity implements View.OnTouchListener {

public static List<RowItem> rowItems = new ArrayList<RowItem>(); 

OnCreate(){
   //and perform the action you want to do.  

}
//Hope this will help you definately.
ArrayList rowItems = new ArrayList();
Intent intent = new Intent(SplashScreen.this, MainActivity.class);

intent.putExtra("key",array); startActivity(intent);

//intent.putExtra("key",array); will show error if your Model class is not  implements Serializable  eg: public class Model  implements Serializable{

String id;
String price;
String name;
//generate your getter setter and set data in to this.
}

//For getting data in to another class just use

ArrayList<Model> data = (ArrayList<Model>)getIntent().getSerializable("key");