Android:无法启动活动

时间:2014-02-02 11:50:02

标签: android android-intent

当我点击应用程序中的按钮时,我在日志中收到以下错误 我将发布发送活动和接收活动的代码。

发送活动: `ImageButton Tracker,History,Planner,Settings,AboutUs;

TrackerDAO getArivalDate;

long getlastID;
String getAD;
String Check;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);

    Tracker = (ImageButton) findViewById(R.id.imageButton_triptrackerbutton);
    Tracker.setOnClickListener(this);

    History = (ImageButton) findViewById(R.id.imageButton_triphistorybutton);
    History.setOnClickListener(this);

    Planner = (ImageButton) findViewById(R.id.imageButton_tripplannerbutton);
    Planner.setOnClickListener(this);

    Settings = (ImageButton) findViewById(R.id.imageButton_settings);
    Settings.setOnClickListener(this);

    AboutUs = (ImageButton) findViewById(R.id.imageButton_aboutus);
    AboutUs.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.imageButton_triptrackerbutton:

        checkForNowGoingTour();

        break;

    case R.id.imageButton_triphistorybutton:

        Intent OpenTripHistory = new Intent (MainMenuActivity.this, HistoryListActivity.class);
        startActivity(OpenTripHistory);

        break;
    case R.id.imageButton_tripplannerbutton:

        Intent OpenTripPLanner = new Intent (MainMenuActivity.this, PlannerListwithCreateNewActivity.class);
        startActivity(OpenTripPLanner);

        break;
    case R.id.imageButton_settings:

        break;
    case R.id.imageButton_aboutus:

        break;
    }

}

private void checkForNowGoingTour() {

    try{
        getArivalDate = new TrackerDAO(MainMenuActivity.this);

    }catch(SQLException e){

        e.printStackTrace();
    }

    getArivalDate.open();
    getlastID = getArivalDate.getMaxID();
    getAD = getArivalDate.getArivaldateforCheck(getlastID);
    getArivalDate.close();

    Check = getAD + " ifDatabaseIsEmpty"; //When Database is empty a value [ ifDatabaseIsEmpty] is needed to check contentEquals

    if (Check.contentEquals("Profile on Running ifDatabaseIsEmpty")){

        Intent OpenTrackerActivity = new Intent (MainMenuActivity.this, TrackerMenuActivity.class);
        startActivity(OpenTrackerActivity); 

    }else{

        AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(MainMenuActivity.this);

        alertDialog2.setTitle("Profile not created yet!"); // Setting Dialog Title

        alertDialog2.setMessage("No profile is running, are you want to create New Profile?"); // Setting Dialog Message

        alertDialog2.setIcon(R.drawable.alerticon); // Setting Icon to Dialog

        // Setting "Yes" Button
        alertDialog2.setPositiveButton("YES",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {

                Intent OpenCreateActivity = new Intent(MainMenuActivity.this, CreateProfile_1Activity.class);
                startActivity(OpenCreateActivity);

            }
        });

        // Setting "NO" Button
        alertDialog2.setNegativeButton("NO",new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        alertDialog2.show(); // Showing Alert Dialog

    }
}

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

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_DOWN){
        switch(keyCode){

        case KeyEvent.KEYCODE_BACK:

            AlertDialog.Builder alertDialog3 = new AlertDialog.Builder(MainMenuActivity.this);

            alertDialog3.setTitle("Are you Sure?"); // Setting Dialog Title

            alertDialog3.setMessage("Click yes to exit!"); // Setting Dialog Message

            alertDialog3.setIcon(R.drawable.alerticon); // Setting Icon to Dialog

            // Setting "Yes" Button
            alertDialog3.setPositiveButton("YES",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                    finish();
                    System.exit(0);

                }
            });

            // Setting "NO" Button
            alertDialog3.setNegativeButton("NO",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });

            alertDialog3.show(); // Showing Alert Dialog    

        }

    }
    return super.onKeyDown(keyCode, event);
}

}

Here The Activity that has to open after that call: PlannerDAO DBHelper;

SimpleCursorAdapter DataAdapter;

ImageButton AddNew;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_planner_listwith_create_new);

    AddNew = (ImageButton) findViewById(R.id.imageButton_PlannerListAddNew);
    AddNew.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            Intent OpenAddNew = new Intent(PlannerListwithCreateNewActivity.this, AddNewTripActivity.class);
            startActivity(OpenAddNew);

        }
    });

    try{

    DBHelper = new PlannerDAO(this);

    }catch(SQLException e){

        e.printStackTrace();
    }

    DBHelper.open();

    displayListView();
}

private void displayListView() {

    Cursor PlannerCursor = DBHelper.fetchAllPlannerEntries();

    // The desired columns to be bound
    String[] PlannerColumns = new String[] {
                DBConfiguration.KEY_PLANNERNAME,
                DBConfiguration.KEY_PLANNERCITY,
                DBConfiguration.KEY_PLANNERDATE,
                DBConfiguration.KEY_PLANNERTRAVELBY
              };
    // the XML defined views which the data will be bound to 

    int[] XML = new int[]{
             R.id.TV_Plan_Name,
             R.id.TV_Plan_City,
             R.id.TV_Plan_date,
             R.id.TV_Plan_TravelBy       
    };

    //create the adapter using the PlannerCursor pointing to the desired data 
    //as well as the layout information

    DataAdapter = new SimpleCursorAdapter(PlannerListwithCreateNewActivity.this, R.layout.planner_list_item, PlannerCursor, PlannerColumns, XML, 0);

    ListView listView = (ListView) findViewById(R.id.listView_PlannerList);

    //listView.setEmptyView(findViewById(R.id.textView_HIstoryListforEmpty));
     // Assign adapter to ListView
    listView.setAdapter(DataAdapter);

    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> listView, View view, int position, long id) {

            //DisplayInfoMethod(id);    

        }
    });

}

private void DisplayInfoMethod(long id) {



}

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

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(event.getAction() == KeyEvent.ACTION_DOWN){
        switch(keyCode){

        case KeyEvent.KEYCODE_BACK:
            Intent BacktoAddNew = new Intent(PlannerListwithCreateNewActivity.this, MainMenuActivity.class);
            startActivity(BacktoAddNew);

        }

    }
    return super.onKeyDown(keyCode, event);
}

}

` 这是logCat:

02-02 06:47:29.490: E/AndroidRuntime(831): FATAL EXCEPTION: main
02-02 06:47:29.490: E/AndroidRuntime(831): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blackcat.triporganizer/com.blackcat.triporganizer.planner.PlannerListwithCreateNewActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
02-02 06:47:29.490: E/AndroidRuntime(831):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
02-02 06:47:29.490: E/AndroidRuntime(831):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-02 06:47:29.490: E/AndroidRuntime(831):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-02 06:47:29.490: E/AndroidRuntime(831):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
02-02 06:47:29.490: E/AndroidRuntime(831):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 06:47:29.490: E/AndroidRuntime(831):  at android.os.Looper.loop(Looper.java:137)
02-02 06:47:29.490: E/AndroidRuntime(831):  at android.app.ActivityThread.main(ActivityThread.java:5103)
02-02 06:47:29.490: E/AndroidRuntime(831):  at java.lang.reflect.Method.invokeNative(Native Method)
02-02 06:47:29.490: E/AndroidRuntime(831):  at java.lang.reflect.Method.invoke(Method.java:525)
02-02 06:47:29.490: E/AndroidRuntime(831):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-02 06:47:29.490: E/AndroidRuntime(831):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-02 06:47:29.490: E/AndroidRuntime(831):  at dalvik.system.NativeStart.main(Native Method)
02-02 06:47:29.490: E/AndroidRuntime(831): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
02-02 06:47:29.490: E/AndroidRuntime(831):  at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303)
02-02 06:47:29.490: E/AndroidRuntime(831):  at android.widget.CursorAdapter.init(CursorAdapter.java:168)
02-02 06:47:29.490: E/AndroidRuntime(831):  at android.widget.CursorAdapter.<init>(CursorAdapter.java:145)
02-02 06:47:29.490: E/AndroidRuntime(831):  at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:91)
02-02 06:47:29.490: E/AndroidRuntime(831):  at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
02-02 06:47:29.490: E/AndroidRuntime(831):  at com.blackcat.triporganizer.planner.PlannerListwithCreateNewActivity.displayListView(PlannerListwithCreateNewActivity.java:82)
02-02 06:47:29.490: E/AndroidRuntime(831):  at com.blackcat.triporganizer.planner.PlannerListwithCreateNewActivity.onCreate(PlannerListwithCreateNewActivity.java:56)
02-02 06:47:29.490: E/AndroidRuntime(831):  at android.app.Activity.performCreate(Activity.java:5133)
02-02 06:47:29.490: E/AndroidRuntime(831):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-02 06:47:29.490: E/AndroidRuntime(831):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
02-02 06:47:29.490: E/AndroidRuntime(831):  ... 11 more

你能帮我找出错误吗?

1 个答案:

答案 0 :(得分:2)

在您的LogCat列表中,您可以找到: java.lang.IllegalArgumentException:column&#39; _id&#39;不存在,但SimpleCursorAdapter需要 _id 字段

  

处理内容URI ID

     

按照惯例,提供商提供对表中单行的访问权限   接受内容URI,其中包含该行末尾的行ID值   URI。同样按照惯例,提供商将ID值与表格进行匹配   _ID列,并对匹配的行执行请求的访问。

     

此约定有助于应用程序访问的通用设计模式   提供者。该应用程序对提供程序进行查询并显示   使用CursorAdapter在ListView中生成Cursor。定义   CursorAdapter需要Cursor中的一列为_ID

     

然后,用户从UI中选择一个显示的行   查看或修改数据。该应用程序从中获取相应的行   支持ListView的Cursor获取此行的_ID值,   将其附加到内容URI,并将访问请求发送到   供应商。然后,提供程序可以执行查询或修改   用户选择的确切行。

http://developer.android.com/guide/topics/providers/content-provider-creating.html#ContentURI