当我尝试在我的应用程序中打开一个活动时,它显示一些已保存的消息(我保存在同一个应用程序中的另一个活动中),它强制关闭并在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
在我使用此LINK中的代码之前这是正常的但是在我使用代码之后,我的数据库column
现在没有在我尝试打开的活动中打开。(这意味着要显示消息)。
Mensagenssalvas.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);
}
}
答案 0 :(得分:1)
您需要检查Mensagenssalvas.java的第41行 并找出导致NullPointerException的原因。
在很多情况下,你只是忘了初始化它。
修复: 不要将您想要使用的内容设置为NULL, 取而代之的是,将其设置为0或“”,具体取决于它的类型。 您需要设置具有“EMPTY”或“ZERO”值但不是NULL的内容。