获取错误:无法实例化活动

时间:2015-04-20 11:17:58

标签: android

我一直在关注本教程:http://developer.android.com/guide/topics/ui/menus.html#CAB。但是,当我运行应用程序时,出现错误:

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

这是我的MainActivity。在我在label 1和`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);

}

}

编辑1:这一行

public class MainActivity extends ActionBarActivity implements AbsListView.MultiChoiceModeListener{

在Android Studio中向我显示错误,当我点击Alt + Enter时,它向我展示了两个选项:使类抽象,或实现其方法。

第一个选项给了我最初的问题,所以现在我选择了2.这是实现的其他方法:

 @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) {

}

现在,我怀疑:这些是LABEL 1和LABEL 2之间完全相同的方法,删除任何一组都会导致错误。我该怎么办?

1 个答案:

答案 0 :(得分:1)

您收到此错误:Unable to instantiate activity因为您进行了活动abstract。删除它并实现AbsListView.MultiChoiceModeListener interface。

的方法

当您实现接口时,必须实现列出的所有方法。如果您不知道如何处理方法,请不要使用它们原样。

  

现在,我怀疑:这些是LABEL 1和LABEL 2之间完全相同的方法,删除任何一组都会导致错误。我该怎么办?

你不应该有两个相同的。尝试对您实现的方法使用@Override注释。