0

All i am trying to do is click on button to go to another activity through intent. But when i click on button it shows following error in my logcat.

11-23 22:14:21.761 7140-7140/com.example.admin.mysqlitest E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to resume activity {com.example.admin.mysqlitest/com.example.admin.mysqlitest.InsertActivity}: java.lang.NullPointerException at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124) at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)

My code for both MainActivity and InsertActivty are here.

Main Activity

public class MainActivity  extends ListActivity  {

    private StudentOperation studentDBoperation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Button   btListe;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        studentDBoperation = new StudentOperation(this);
        studentDBoperation.open();

        List values = studentDBoperation.getAllStudents();

        // Use the SimpleCursorAdapter to show the
        // elements in a ListView
        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, values);
        setListAdapter(adapter);

     btListe = (Button)findViewById(R.id.newBtn);
        btListe.setOnClickListener(new OnClickListener()
        {    public void onClick(View v)
            {
            // Intent intent = new Intent(main.this, ListViewImage.class);
                Intent intent = new Intent(MainActivity.this,InsertActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
 @Override
    protected void onResume() {
        studentDBoperation.open();
        super.onResume();
    }

    @Override
    protected void onPause() {
        studentDBoperation.close();
        super.onPause();
    }

}

Insert Activity

public class InsertActivity extends ListActivity {

    private StudentOperation studentDBoperation;



protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.insert_activity);
    }
    public void addUser(View view) {

        ArrayAdapter adapter = (ArrayAdapter) getListAdapter();

        EditText text = (EditText) findViewById(R.id.editText1);
        Students stud = studentDBoperation.addStudent(text.getText().toString());

        adapter.add(stud);
        if(true)
        {
            Toast.makeText(getApplicationContext(), (String)stud.getName()+"is added successfully!!", Toast.LENGTH_LONG).show();
            Intent intent = new Intent(InsertActivity.this,MainActivity.class);
            startActivity(intent);
            finish();
        }

    }

    public void deleteFirstUser(View view) {

        ArrayAdapter adapter = (ArrayAdapter) getListAdapter();
        Students stud = null;

        if (getListAdapter().getCount() > 0) {
            stud = (Students) getListAdapter().getItem(0);
            studentDBoperation.deleteStudent(stud);
            adapter.remove(stud);
        }

    }

    @Override
    protected void onResume() {
        studentDBoperation.open();
        super.onResume();
    }

    @Override
    protected void onPause() {
        studentDBoperation.close();
        super.onPause();
    }
}

Any suggestions please. I am new to Android.

tabia
  • 631
  • 2
  • 10
  • 33

1 Answers1

0

In InsertActivity, studentDBoperation is not initialised and hence you might be getting NPE. Also, you should override onCreate() and setContentView() just like you have in MainActivity.

Suhas
  • 1,451
  • 14
  • 22