当我尝试在我的应用程序中打开一个活动时,它应该显示一些已保存的消息(我保存在同一应用程序的另一个活动中)它强制关闭并在 LogCat 中向我显示这些错误:
10-08 18:22:17.243: E/AndroidRuntime(269): FATAL EXCEPTION: main
10-08 18:22:17.243: E/AndroidRuntime(269): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mensagem/com.example.mensagem.Mensagenssalvas}: java.lang.NullPointerException
10-08 18:22:17.243: E/AndroidRuntime(269): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-08 18:22:17.243: E/AndroidRuntime(269): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-08 18:22:17.243: E/AndroidRuntime(269): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-08 18:22:17.243: E/AndroidRuntime(269): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-08 18:22:17.243: E/AndroidRuntime(269): at android.os.Handler.dispatchMessage(Handler.java:99)
10-08 18:22:17.243: E/AndroidRuntime(269): at android.os.Looper.loop(Looper.java:123)
10-08 18:22:17.243: E/AndroidRuntime(269): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-08 18:22:17.243: E/AndroidRuntime(269): at java.lang.reflect.Method.invokeNative(Native Method)
10-08 18:22:17.243: E/AndroidRuntime(269): at java.lang.reflect.Method.invoke(Method.java:521)
10-08 18:22:17.243: E/AndroidRuntime(269): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-08 18:22:17.243: E/AndroidRuntime(269): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-08 18:22:17.243: E/AndroidRuntime(269): at dalvik.system.NativeStart.main(Native Method)
10-08 18:22:17.243: E/AndroidRuntime(269): Caused by: java.lang.NullPointerException
10-08 18:22:17.243: E/AndroidRuntime(269): at com.example.mensagem.Mensagenssalvas.Mensagens(Mensagenssalvas.java:41)
10-08 18:22:17.243: E/AndroidRuntime(269): at com.example.mensagem.Mensagenssalvas.onCreate(Mensagenssalvas.java:26)
10-08 18:22:17.243: E/AndroidRuntime(269): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-08 18:22:17.243: E/AndroidRuntime(269): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-08 18:22:17.243: E/AndroidRuntime(269): ... 11 more
在我使用此链接中的代码之前它是正常的,但在我使用代码之后,我的数据库column
现在没有在我尝试打开的活动中打开。(这是为了显示消息)。
信使萨瓦斯.java
public class Mensagenssalvas extends Activity {
public SQLiteDatabase db;
ArrayList<String> list = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mensagenssalvas);
db = openOrCreateDatabase("banco.db", Context.MODE_WORLD_WRITEABLE, null);
Mensagens();
}
private void Mensagens() {
// TODO Auto-generated method stub
ListView user = (ListView) findViewById(R.id.lvMensagens);
String[] campos = new String[] {"mensagemsalva", "mensagemenviada"};
list = new ArrayList<String>();
Cursor c = db.query( "mensagens", campos, null, null, null, null, null);
c.moveToFirst();
if(c.getCount() > 0) {
while(true) {
list.add(c.getString(c.getColumnIndex("mensagemsalva")).toString());
if(!c.moveToNext()) break;
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, list);
user.setAdapter(adapter);
}
}