Ayushi Jha
Ayushi Jha

Reputation: 4023

Getting error: Unable to Instantiate Activity

I have been following this tutorial: http://developer.android.com/guide/topics/ui/menus.html#CAB. However, when I run the application, I get an error:

java.lang.RuntimeException:
Unable to instantiate activity 
ComponentInfo{com.example.ayushi.chaseyourdream/com.example.ayushi.chaseyourdream.MainActivity}: 
java.lang.InstantiationException: can't instantiate class 
com.example.ayushi.chaseyourdream.MainActivity

Here's my MainActivity. My application was working completely fine before I added code between label 1 and `label 2'.

public abstract class MainActivity extends ActionBarActivity implements AbsListView.MultiChoiceModeListener{

DbHelper db;
ListView myList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    db = new DbHelper(this);
    myList = (ListView) findViewById(R.id.newList);

        loadData();

//    LABEL 1  
myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    myList.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {

        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position,
                                              long id, boolean checked) {
            // Here you can do something when items are selected/de-selected,
            // such as update the title in the CAB
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            // Respond to clicks on the actions in the CAB
            switch (item.getItemId()) {
                case R.id.menu_delete:
                 //   deleteSelectedItems();
                    mode.finish(); // Action picked, so close the CAB
                    return true;
                default:
                    return false;
            }
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            // Inflate the menu for the CAB
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.context_main, menu);
            return true;
        }


        public void onDestroyActionMode(ActionMode mode) {
            // Here you can make any necessary updates to the activity when
            // the CAB is removed. By default, selected items are deselected/unchecked.
        }


        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // Here you can perform updates to the CAB due to
            // an invalidate() request
            return false;
        }
    });

//  LABEL 2

}
public void onResume()
{
    super.onResume();
    loadData();
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
public void loadData()
{
    Cursor cursor = null;
    try {
        cursor = db.fetchData();
    }
    catch(NullPointerException e)
    {
        e.printStackTrace();
    }
     ListAdapter myAdapter = new SimpleCursorAdapter(this, R.layout.tasks,
                cursor,
                new String[]{db._ID, db.COLUMN_1, db.COLUMN_2},
                new int[]{R.id.idnum, R.id.c1, R.id.c2}, 0);
        myList.setAdapter(myAdapter);


}
public void addNew(View v)
{
    Intent intent = new Intent(this,AddActivity.class);
    startActivity(intent);

}

}

EDIT 1: this line

public class MainActivity extends ActionBarActivity implements AbsListView.MultiChoiceModeListener{

showed me an error in Android Studio, and when I clicked Alt + Enter, it showed me two options: Make the class abstract, or implement its methods.

The first option gave me the initial problem, so now I went for option 2. Here are the additional methods implemented :

 @Override
public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {

}

@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
    return false;
}

@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
    return false;
}

@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
    return false;
}

@Override
public void onDestroyActionMode(ActionMode mode) {

}

Now, my doubt is: these are exactly the same methods in between LABEL 1 and LABEL 2, and erasing any one set will lead to errors. What should I do?

Upvotes: 0

Views: 119

Answers (1)

mgokgoz
mgokgoz

Reputation: 186

You are getting this error: Unable to instantiate activity because you made your activity abstract. Remove that and implement the methods of AbsListView.MultiChoiceModeListenerinterface.

When you're implementing an Interface so you have to implement all the methods you have listed. If you don't know what to do with method you don't use just leave them as they are.

Now, my doubt is: these are exactly the same methods in between LABEL 1 and LABEL 2, and erasing any one set will lead to errors. What should I do?

You shouldn't have two of the same. Try using @Override annotation on the methods you implemented.

Upvotes: 1

Related Questions