带有新 Activity 的 ListView OnItemClickListener

ListView OnItemClickListener with a new Activity

我有第一个 activity 代码:

    lvlitems.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //Toast.makeText(BuscarNota.this, "Clicked"+ id, Toast.LENGTH_SHORT).show();
        Intent intent= new Intent();
        intent.setClass(BuscarNota.this, Mostrar.class);
        intent.putExtra("id_nota", id);
        startActivityForResult(intent, 0);
    }
});

然后我有第二个 activity 代码:

EditText nota_input;
MiDB dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mostrar);
    nota_input= (EditText) findViewById(R.id.txtmostrar);

    int prePosition = getIntent().getIntExtra("id_nota", 0);
    Cursor c = dbHandler.notasbyid(prePosition);
    nota_input.setText(c.getString(c.getColumnIndexOrThrow("nota")));
}

但是不起作用...我想要在 EditText 的第二个 activity 中显示 "nota" 的数据,这是一个字符串。

这是日志:

06-09 15:23:55.340: E/AndroidRuntime(1469): FATAL EXCEPTION: main 06-09 15:23:55.340: E/AndroidRuntime(1469): Process: notasDeClase.example.notasdeclase, PID: 1469 06-09 15:23:55.340: E/AndroidRuntime(1469): java.lang.RuntimeException: Unable to start activity ComponentInfo{notasDeClase.example.notasdeclase/notasDeClase.example.notasdeclase.Mostrar}: java.lang.NullPointerException 06-09 15:23:55.340: E/AndroidRuntime(1469): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 06-09 15:23:55.340: E/AndroidRuntime(1469): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 06-09 15:23:55.340: E/AndroidRuntime(1469): at android.app.ActivityThread.access0(ActivityThread.java:135) 06-09 15:23:55.340: E/AndroidRuntime(1469): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 06-09 15:23:55.340: E/AndroidRuntime(1469): at android.os.Handler.dispatchMessage(Handler.java:102) 06-09 15:23:55.340: E/AndroidRuntime(1469): at android.os.Looper.loop(Looper.java:136) 06-09 15:23:55.340: E/AndroidRuntime(1469): at android.app.ActivityThread.main(ActivityThread.java:5017) 06-09 15:23:55.340: E/AndroidRuntime(1469): at java.lang.reflect.Method.invokeNative(Native Method) 06-09 15:23:55.340: E/AndroidRuntime(1469): at java.lang.reflect.Method.invoke(Method.java:515) 06-09 15:23:55.340: E/AndroidRuntime(1469): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 06-09 15:23:55.340: E/AndroidRuntime(1469): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 06-09 15:23:55.340: E/AndroidRuntime(1469): at dalvik.system.NativeStart.main(Native Method) 06-09 15:23:55.340: E/AndroidRuntime(1469): Caused by: java.lang.NullPointerException 06-09 15:23:55.340: E/AndroidRuntime(1469): at notasDeClase.example.notasdeclase.Mostrar.onCreate(Mostrar.java:27) 06-09 15:23:55.340: E/AndroidRuntime(1469): at android.app.Activity.performCreate(Activity.java:5231) 06-09 15:23:55.340: E/AndroidRuntime(1469): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 06-09 15:23:55.340: E/AndroidRuntime(1469): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 06-09 15:23:55.340: E/AndroidRuntime(1469): ... 11 more

你在哪里实例化dbHandler?您确定已正确初始化它吗?你能 post 生成的 SQL 结果吗?你有什么例外吗?

一些提示是:

  • 确保您正确启动了 dbHandler var;
  • 确保 c.getColumnIndexOrThrow("nota") 的结果不同于 -1;
  • 检查 LogCat 中生成的 sql 并尝试找出不一致之处;

我刚刚解决了。谢谢

EditText nota_input;
MiDB dbHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mostrar);

    nota_input= (EditText) findViewById(R.id.txtmostrar);
    dbHandler = new MiDB(this, null, null,1);

    int prePosition =  (int) getIntent().getLongExtra("id_nota", 0);
    Cursor c = dbHandler.notasbyid(prePosition);
    nota_input.setText(c.getString(c.getColumnIndexOrThrow("nota")));

}