ListView OnItemClickListener with a new Activity

163 views Asked by At

I have the first activity with this code:

    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);
    }
});

And then I have the second activity with this code:

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")));
}

But is not working... what I want is to show in the second activity in the EditText the data of "nota" that is a String.

This is the log:

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.access$800(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

2

There are 2 answers

0
dach On BEST ANSWER

i just solve it. thanks

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")));

}
7
E. Fernandes On

Where are you instantiating dbHandler? Are you sure that you have properly initialized it? Can you post the generated SQL result for this? Are you getting any exception?

Some tips are:

  • Make sure you are properly starting the dbHandler var;
  • Make sure that the result of c.getColumnIndexOrThrow("nota") is different of -1;
  • Check out the generated sql in the LogCat and try to spot inconsistencies;