无法恢复活动

7
我是一名新手,正在尝试在我的SQLite数据库更新后允许我的ListView刷新。我在修改了onResume()方法后没有收到编译错误。我正在使用SimpleCursorAdapter进行重新查询,但它不起作用。下面是来自logcat的错误信息,请给予建议……最好提供示例。
Logcat:
  02-19 21:31:49.933: E/AndroidRuntime(714): java.lang.RuntimeException: Unable to resume activity {com.loginplus.home/com.loginplus.home.LoginList}: java.lang.NullPointerException
  02-19 21:31:49.933: E/AndroidRuntime(714):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2444)
  02-19 21:31:49.933: E/AndroidRuntime(714):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2472)
  02-19 21:31:49.933: E/AndroidRuntime(714):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1986)
  02-19 21:31:49.933: E/AndroidRuntime(714):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
  02-19 21:31:49.933: E/AndroidRuntime(714):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
  02-19 21:31:49.933: E/AndroidRuntime(714):    at android.os.Handler.dispatchMessage(Handler.java:99)
  02-19 21:31:49.933: E/AndroidRuntime(714):    at android.os.Looper.loop(Looper.java:137)
  02-19 21:31:49.933: E/AndroidRuntime(714):    at android.app.ActivityThread.main(ActivityThread.java:4424)
  02-19 21:31:49.933: E/AndroidRuntime(714):    at java.lang.reflect.Method.invokeNative(Native Method)
  02-19 21:31:49.933: E/AndroidRuntime(714):    at java.lang.reflect.Method.invoke(Method.java:511)
  02-19 21:31:49.933: E/AndroidRuntime(714):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  02-19 21:31:49.933: E/AndroidRuntime(714):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  02-19 21:31:49.933: E/AndroidRuntime(714):    at dalvik.system.NativeStart.main(Native Method)
  02-19 21:31:49.933: E/AndroidRuntime(714): Caused by: java.lang.NullPointerException
  02-19 21:31:49.933: E/AndroidRuntime(714):    at com.loginplus.home.LoginList.onResume(LoginList.java:101)
  02-19 21:31:49.933: E/AndroidRuntime(714):    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1154)
  02-19 21:31:49.933: E/AndroidRuntime(714):    at android.app.Activity.performResume(Activity.java:4539)
  02-19 21:31:49.933: E/AndroidRuntime(714):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2434)

活动:

     public class LoginList extends Activity implements OnClickListener,    OnItemClickListener {

private ListView loginList;
private Button webLogin;

private ListAdapter loginListAdapter;

private ArrayList<LoginDetails> loginArrayList;

List<String> arrayList = new ArrayList<String>();

@Override 
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    arrayList = populateList();
    loginListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, populateList());
    setContentView(R.layout.login_listview);


    loginList = (ListView)
   findViewById(R.id.loginlist);
    loginList.setOnItemClickListener(this);

    webLogin = (Button)
   findViewById(R.id.button3);
    webLogin.setOnClickListener(this);


}

@Override
public void onClick (View v) {
    Intent webLoginIntent = new Intent (this, LoginPlusActivity.class);
    startActivity(webLoginIntent);

}

public List<String> populateList (){

    List<String> webNameList = new ArrayList<String>();

    dataStore openHelperClass = new dataStore (this);

    SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();

    Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, dataStore.COLUMN_NAME_SITE, null);

    startManagingCursor(cursor);


    while (cursor.moveToNext()){
    String sName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_SITE));
    String wUrl = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_ADDRESS));
    String uName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_USERNAME));
    String pWord = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_PASSWORD));
    String lNotes = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_NOTES));

    LoginDetails lpDetails = new LoginDetails();

        lpDetails.setsName(sName);
        lpDetails.setwUrl(wUrl);
        lpDetails.setuName(uName);
        lpDetails.setpWord(pWord);
        lpDetails.setlNotes(lNotes);

        loginArrayList.add(lpDetails);
        webNameList.add(sName);
}

sqliteDatabase.close();
return webNameList;
}



@Override
protected void onResume() {
    super.onResume();

    loginArrayList.clear();

    arrayList.clear();

    arrayList = populateList();

    dataStore refreshHelper = new dataStore (this);
    SQLiteDatabase sqliteDatabase = refreshHelper.getWritableDatabase();
    Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, dataStore.COLUMN_NAME_SITE, null);
    String[]columns = new String[] { dataStore.COLUMN_NAME_SITE, dataStore.COLUMN_NAME_ADDRESS, dataStore.COLUMN_NAME_USERNAME, dataStore.COLUMN_NAME_PASSWORD, dataStore.COLUMN_NAME_NOTES };
    int[] to = new int[]{R.id.rusName, R.id.ruwUrl, R.id.ruuName, R.id.rupWord, R.id.ruNotes};
    SimpleCursorAdapter loginListAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, columns, to);
    loginListAdapter.notifyDataSetChanged();

}

@Override
public void onItemClick(AdapterView<?> arg0 , View arg1, int arg2, long arg3) {
    Toast.makeText(getApplicationContext(), "Selected ID :" + arg2, Toast.LENGTH_SHORT).show();

    Intent updateDeleteLoginInfo = new Intent (this, UpdateDeleteLoginList.class);



    LoginDetails clickedObject = loginArrayList.get(arg2);

        Bundle loginBundle = new Bundle();
    loginBundle.putString("clickedWebSite",clickedObject.getsName());
    loginBundle.putString("clickedWebAddress",clickedObject.getwUrl());
    loginBundle.putString("clickedUserName",clickedObject.getuName());
    loginBundle.putString("clickedPassWord",clickedObject.getpWord());
    loginBundle.putString("clickedNotes",clickedObject.getlNotes());

    updateDeleteLoginInfo.putExtras(loginBundle);

    startActivityForResult(updateDeleteLoginInfo, 0);       
       }    
          }

RennoDiniro 编辑结果:

日志记录:

       02-21 23:40:18.419: E/AndroidRuntime(705): FATAL EXCEPTION: main
       02-21 23:40:18.419: E/AndroidRuntime(705): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.loginplus.home/com.loginplus.home.LoginList}: java.lang.NullPointerException
       02-21 23:40:18.419: E/AndroidRuntime(705):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
       02-21 23:40:18.419: E/AndroidRuntime(705):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
       02-21 23:40:18.419: E/AndroidRuntime(705):   at android.app.ActivityThread.access$600(ActivityThread.java:123)
       02-21 23:40:18.419: E/AndroidRuntime(705):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
       02-21 23:40:18.419: E/AndroidRuntime(705):   at android.os.Handler.dispatchMessage(Handler.java:99)
       02-21 23:40:18.419: E/AndroidRuntime(705):   at android.os.Looper.loop(Looper.java:137)
       02-21 23:40:18.419: E/AndroidRuntime(705):   at android.app.ActivityThread.main(ActivityThread.java:4424)
       02-21 23:40:18.419: E/AndroidRuntime(705):   at java.lang.reflect.Method.invokeNative(Native Method)
       02-21 23:40:18.419: E/AndroidRuntime(705):   at java.lang.reflect.Method.invoke(Method.java:511)
       02-21 23:40:18.419: E/AndroidRuntime(705):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
       02-21 23:40:18.419: E/AndroidRuntime(705):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
       02-21 23:40:18.419: E/AndroidRuntime(705):   at dalvik.system.NativeStart.main(Native Method)
       02-21 23:40:18.419: E/AndroidRuntime(705): Caused by: java.lang.NullPointerException
       02-21 23:40:18.419: E/AndroidRuntime(705):   at com.loginplus.home.LoginList.populateList(LoginList.java:88)
       02-21 23:40:18.419: E/AndroidRuntime(705):   at com.loginplus.home.LoginList.onCreate(LoginList.java:37)
       02-21 23:40:18.419: E/AndroidRuntime(705):   at android.app.Activity.performCreate(Activity.java:4465)
       02-21 23:40:18.419: E/AndroidRuntime(705):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
       02-21 23:40:18.419: E/AndroidRuntime(705):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)

活动类:

      public class LoginList extends Activity implements OnClickListener,    OnItemClickListener {

      private ListView loginList;
      private Button webLogin;

      private ListAdapter loginListAdapter;

      private ArrayList<LoginDetails> loginArrayList;

      List<String> arrayList = new ArrayList<String>();

      @Override 
      public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      loginListAdapter = new ArrayAdapter<String>(this,   android.R.layout.simple_list_item_1, populateList());
      arrayList = populateList(); 
      setContentView(R.layout.login_listview);


      loginList = (ListView)
      findViewById(R.id.loginlist);
      loginList.setOnItemClickListener(this);

      webLogin = (Button)
      findViewById(R.id.button3);
      webLogin.setOnClickListener(this);
      }

      @Override
      public void onClick (View v) {
      Intent webLoginIntent = new Intent (this, LoginPlusActivity.class);
      startActivity(webLoginIntent);
      }

      public List<String> populateList (){

      List<String> webNameList = new ArrayList<String>();

      dataStore openHelperClass = new dataStore (this);

      SQLiteDatabase sqliteDatabase = openHelperClass.getReadableDatabase();

      Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, dataStore.COLUMN_NAME_SITE, null);

      startManagingCursor(cursor);


      while (cursor.moveToNext()){
      String sName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_SITE));
      String wUrl = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_ADDRESS));
      String uName = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_USERNAME));
      String pWord = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_PASSWORD));
      String lNotes = cursor.getString(cursor.getColumnIndex(dataStore.COLUMN_NAME_NOTES));

    LoginDetails lpDetails = new LoginDetails();

   lpDetails.setsName(sName);
   lpDetails.setwUrl(wUrl);
   lpDetails.setuName(uName);
   lpDetails.setpWord(pWord);
   lpDetails.setlNotes(lNotes);

   loginArrayList.add(lpDetails);
   webNameList.add(sName);
   }

   sqliteDatabase.close();
   return webNameList;
    }

   @Override
   protected void onResume() {
    super.onResume();

    try{
    loginArrayList = new ArrayList<LoginDetails>();
    arrayList = new ArrayList<String>();
    loginArrayList.clear();
    arrayList.clear();

    arrayList = populateList();

    dataStore refreshHelper = new dataStore (this);
    SQLiteDatabase sqliteDatabase = refreshHelper.getWritableDatabase();
    Cursor cursor = sqliteDatabase.query(dataStore.TABLE_NAME_INFOTABLE, null, null, null, null, null, dataStore.COLUMN_NAME_SITE, null);
    String[]columns = new String[] { dataStore.COLUMN_NAME_SITE, dataStore.COLUMN_NAME_ADDRESS, dataStore.COLUMN_NAME_USERNAME, dataStore.COLUMN_NAME_PASSWORD, dataStore.COLUMN_NAME_NOTES };
     int[] to = new int[]{R.id.rusName, R.id.ruwUrl, R.id.ruuName, R.id.rupWord, R.id.ruNotes};
    SimpleCursorAdapter loginListAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, columns, to);
    loginListAdapter.notifyDataSetChanged();
     }catch(Exception e)
    {
   e.printStackTrace();
     }
    }
    @Override
    public void onItemClick(AdapterView<?> arg0 , View arg1, int arg2, long arg3) {
    Toast.makeText(getApplicationContext(), "Selected ID :" + arg2,    Toast.LENGTH_SHORT).show();

    Intent updateDeleteLoginInfo = new Intent (this, UpdateDeleteLoginList.class);



    LoginDetails clickedObject = loginArrayList.get(arg2);

    Bundle loginBundle = new Bundle();
    loginBundle.putString("clickedWebSite",clickedObject.getsName());
    loginBundle.putString("clickedWebAddress",clickedObject.getwUrl());
    loginBundle.putString("clickedUserName",clickedObject.getuName());
    loginBundle.putString("clickedPassWord",clickedObject.getpWord());
    loginBundle.putString("clickedNotes",clickedObject.getlNotes());

    updateDeleteLoginInfo.putExtras(loginBundle);

    startActivityForResult(updateDeleteLoginInfo, 0);   
   }
   }

你的onPause()在哪里? - Sergey Benner
你的onResume方法中,第101行是哪一行? - Nickolaus
第101行是loginArrayList.clear(); - Nickolaus - user1165694
1
我不想显而易见,但这意味着loginArrayList是null。因此,如果您没有在onPause()方法中保存对象的状态,则无需清除它。您应该更仔细地研究Activiy生命周期---http://developer.android.com/reference/android/app/Activity.html。没有必要清除“null”对象-它们未初始化。除了`arrayList`之外,此类中的所有私有成员变量在您恢复onResume()事件时都为null。希望这能让您对此有一些了解。 - Sergey Benner
7个回答

3

使用指向所需数据的游标以及布局信息创建适配器。

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, columns, to);

在您的情况下,由于您使用了R.layout.simple_list_item_1

columns将为null从游标获取的特定列数据

to将为android.R.id.text1


例如:

  Cursor cursor = getContentResolver().query(People.CONTENT_URI, new String[]{People._ID, People.NAME, People.NUMBER}, null, null, null);
  startManagingCursor(cursor);
   // THE DESIRED COLUMNS TO BE BOUND

   String[] columns = new String[] { People.NAME, People.NUMBER };

  // THE XML DEFINED VIEWS WHICH THE DATA WILL BE BOUND TO
   int[] to = new int[] { R.id.name_entry, R.id.number_entry };

       SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,R.layout.list_example_entry, cursor, columns, to);

感谢您的建议。我已经实施了它们,但现在出现了NullPointerException异常。请查看修订后的代码。 - user1165694

3

显然,您没有初始化ArrayList。但是……针对您的情况,考虑使用Loader。在ApiDemos中有完整的示例(列在Loader页面底部)。


1
注意,您从未实例化loginArrayList,因此当您尝试在onResume()中访问它时,它为null,因此会崩溃。在访问之前进行实例化。
更新于22/02/2013: 啊,您忘记了在应用程序首次运行时实例化loginArrayList。 在onCreate()中,在super.OnCreate(...)之后放置以下代码。
loginArrayList = new ArrayList<LoginDetails>();

所以它看起来像这样:

loginArrayList = new ArrayList<LoginDetails>();
loginListAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,populateList());
arrayList = populateList(); 
setContentView(R.layout.login_listview);


//rest of your code...

以下是使您的应用程序运行的快速调整,但请注意,要解决整个问题需要更深入地分析在整个暂停和恢复过程中失去了哪些信息。

在您的onResume()中执行以下操作:

try{

// your code

}catch(Exception e)
{
 //Have the printStackTrace to the problems see what's going on without crashing.
 //e.printStackTrace();
}

但这还没有解决问题, 在onResume()中, 请执行以下操作:
  • 当您第一次获取数据时,请将其存储到数据库中。
  • 在 onResume() 中重新创建列表,并使用存储在数据库中的数据进行更新。
因此,让我们开始吧。
@Override
protected void onResume() {
    super.onResume();


loginArrayList = new ArrayList<LoginDetails>();
arrayList = new ArrayList<String>();

arrayList = populateList();

// any other code you require to be done after the list is populated.
}

祝你好运。


我已经在上面添加了你的更改的代码,还附带了logcat错误信息。 - user1165694
在哪里?我认为您没有更新它,因为问题看起来与旧代码相同... - rennoDeniro
我使用编辑工具将我的修改后的代码添加到了你的帖子中,但是没有显示出来。现在我已经将所有内容都添加到了我的帖子中。第88行:loginArrayList.add(lpDetails); 第37行:loginListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, populateList()); - user1165694

0

看起来游标为空,快速修复此问题并使您的应用程序运行的方法是更改:

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor,null,null);
mAdapter.notifyDataSetChanged();

if (cursor != null) {
   SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor,null,null);
   mAdapter.notifyDataSetChanged();
}

否则,你的光标可能为空,查询时可能会选择错误的投影。

使用您的建议后仍然收到相同的错误。02-12 14:48:38.904: E/AndroidRuntime(646):Caused by: java.lang.NullPointerException 02-12 14:48:38.904: E/AndroidRuntime(646): at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:327) 02-12 14:48:38.904: E/AndroidRuntime(646): at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:81) 02-12 14:48:38.904: E/AndroidRuntime(646): at com.loginplus.home.LoginList.onResume(LoginList.java:105) 02-12 14:48:38.904: E/AndroidRuntime(646): - user1165694

0

问题出在托管光标 -

startManagingCursor(cursor);

请尝试自己维护光标,而不是使用已弃用的“activity managed”光标。

我两天前管理光标后在onResume调用时遇到了完全相同的问题。


0

在SimpleCursorAdapter构造函数中,您需要提供从列名到资源ID的映射 - 也就是fromto参数,您目前正在将它们设置为null。


0
你从未初始化loginArrayList(因此它是null)。

用以下代码初始化loginArrayList: ArrayList loginArrayList = new ArrayList<LoginDetails>(); ......现在收到logcat错误: 02-19 22:47:29.336: E/AndroidRuntime(1160): Caused by: java.lang.NullPointerException 02-19 22:47:29.336: E/AndroidRuntime(1160): at com.loginplus.home.LoginList.populateList(LoginList.java:87)...第87行是loginArrayList.add(lpDetails); - user1165694
@user1165694 在第87行之前尝试这个代码:if (loginArrayList == null){Log.d("test", "loginArrayList is NULL");}else{if(lpDetails == null){Log.d("test", "lpDetails is NULL");}}。logcat 会打印什么? - Phil
当我在第 87 行上面添加这段代码时,我收到了一条黄线,指向 {Log.d("test", "lpDetails is NULL");}},提示:死代码? - user1165694
@user1165694,你还需要有初始化“loginArrayList”的代码行。 - Phil
除了一个 LoginArrayList 是 Null 的消息,我的 logcat 看起来在使用 Log.d 实现后仍然相同。 - user1165694
如果你收到了“loginArrayList is NULL”的消息,那么你的错误就是由此引起的。你不能使用null的loginArrayList调用loginArrayList.clear()。尝试将loginArrayList = new ArrayList<LoginDetails>();这一行移到onCreate()方法的最后一行。 - Phil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接