mhtamun
mhtamun

Reputation: 75

Android: Unable to start an activity

I get the below error in the log when I click a button in my application.
I will post the code for the Sending Activity and the receiving activity.

Sending Activity: `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);
}

}

` Here Is the 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

Can you Please help me to find out the mistake?

Upvotes: 0

Views: 107

Answers (1)

michalu
michalu

Reputation: 118

In your LogCat listing you can find: java.lang.IllegalArgumentException: column '_id' does not exist, but SimpleCursorAdapter need a _id field

Handling content URI IDs

By convention, providers offer access to a single row in a table by accepting a content URI with an ID value for the row at the end of the URI. Also by convention, providers match the ID value to the table's _ID column, and perform the requested access against the row that matches.

This convention facilitates a common design pattern for apps accessing a provider. The app does a query against the provider and displays the resulting Cursor in a ListView using a CursorAdapter. The definition of CursorAdapter requires one of the columns in the Cursor to be _ID

The user then picks one of the displayed rows from the UI in order to look at or modify the data. The app gets the corresponding row from the Cursor backing the ListView, gets the _ID value for this row, appends it to the content URI, and sends the access request to the provider. The provider can then do the query or modification against the exact row the user picked.

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

Upvotes: 2

Related Questions