2012-12-03 59 views
2

我想在Eclipse中编写代码java android将两个字段输入到数据库中。在数据库中有3行条目后,我点击“查看列表”数据库,并出现错误。我已经收窄我想下到错误的DetailActivity.javaIllegalStateException:无法执行活动的方法

Intent intent = new Intent(this, RaceListActivity.class); 

存在一个RaceListActivity.classRaceListActivity.java.但我得到这个错误。 DetailActivity.javaRaceListActivity.java代码位于错误退出的位置。 LogCat低于此值。

详细Activity.java

// Shows/edits the data for one row. 
public class DetailActivity extends Activity { 
    private RaceDB mDB; 
    private Long mRowId; 
    private EditText mEditText1; 
    private EditText mEditText2; 
    private CheckBox mCheckBox; 
    private static final String TAG = "INFORMATION"; 

    @Override 
    protected void onCreate(Bundle bundle) { 
     super.onCreate(bundle); 

     setContentView(R.layout.activity_main); 

     mEditText1 = (EditText) findViewById(R.id.editText1); 
     mEditText2 = (EditText) findViewById(R.id.editText2); 
     mCheckBox = (CheckBox) findViewById(R.id.checkBox1); 

     mRowId = null; 

     if (bundle == null) { // initially, Intent -> extras -> rowID 
      Bundle extras = getIntent().getExtras(); 
      if (extras != null && extras.containsKey(RaceListActivity.EXTRA_ROWID)) { 
       mRowId = extras.getLong(RaceListActivity.EXTRA_ROWID); 
      } 
     } 
     else { // tricky: recover mRowId from kill destroy/create cycle 
      mRowId = bundle.getLong(SAVE_ROW); 
     } 

     mDB = new RaceDB(this); 
     mDB.open(); 
     Log.d(TAG, "Database is now open and saved"); 
     dbToUI(); 
     Log.d(TAG, "Database is now able to be shown"); 
     onPause(); 
    }  

    public void view_races(View v){ 
     Log.d(TAG, "Database is now almost shown"); 
     Intent intent = new Intent(this, RaceListActivity.class); 
     startActivity(intent);  
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     mDB.close(); 
    } 

    // Copies database state up to the UI. 
    private void dbToUI() { 
     if (mRowId != null) { 
      Cursor cursor = mDB.query(mRowId); 
      mEditText1.setText(cursor.getString(RaceDB.INDEX_TITLE)); 
      mEditText2.setText(cursor.getString(RaceDB.INDEX_BODY)); 
      mCheckBox.setChecked(cursor.getInt(RaceDB.INDEX_STATE) > 0); 

      cursor.close(); 
     } 
    } 

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


    /** Save the state in the UI to the database, creating a new row or updating 
    * an existing row. 
    */ 
    private void save() { 
     String title = mEditText1.getText().toString(); 
     String body = mEditText2.getText().toString(); 
     int done = 0; 
     if (mCheckBox.isChecked()) done = 1; 

     // Not null = edit of existing row, or it's new but we saved it previously, 
     // so now it has a rowId anyway. 
     if (mRowId != null) { 
      mDB.updateRow(mRowId, mDB.createContentValues(title, body, done)); 
     } 
     else { 
      mRowId = mDB.createRow(mDB.createContentValues(title, body, done)); 
     } 
    } 

    public static final String SAVE_ROW = "saverow"; 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     outState.putLong(SAVE_ROW, mRowId); 
    } 
} 

RaceListActivity.java

// Main activity -- shows data list, has a few controls. 
public class RaceListActivity extends ListActivity { 
    private RaceDB mDB; // Our connection to the database. 
    private SimpleCursorAdapter mCursorAdapter; 
    private static final String TAG = "INFORMATION"; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 

     Log.d(TAG, "Database is now shown"); 
     setContentView(R.layout.activity_view_races); 

/*  Button button = (Button) findViewById(R.id.button1); 
     button.setOnClickListener(new OnClickListener() { 
      public void onClick(View v) { 
       startDetail(0, true); // true = create new 
      } 
     });*/ 

     // Start up DB connection (closed in onDestroy). 


     mDB = new RaceDB(this); 
     mDB.open(); 

     // Get the "all rows" cursor. startManagingCursor() is built in for the common case, 
     // takes care of closing etc. the cursor. 
     Cursor cursor = mDB.queryAll(); 
     startManagingCursor(cursor); 

     // Adapter: maps cursor keys, to R.id.XXX fields in the row layout. 
     String[] from = new String[] { RaceDB.KEY_TITLE, RaceDB.KEY_STATE }; 
     int[] to = new int[] { R.id.rowtext, R.id.rowtext2 }; 
     mCursorAdapter = new SimpleCursorAdapter(this, R.layout.row2, cursor, from, to); 

     // Map "state" int to text in the row -- intercept the setup of each row view, 
     // fiddle with the data for the state column. 
     mCursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { 
      public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 
       if (columnIndex == RaceDB.INDEX_STATE) { 
        TextView textView = (TextView) view; 
        if (cursor.getInt(RaceDB.INDEX_STATE) > 0) { 
         textView.setText(" (done) "); 
        } 
        else { 
         textView.setText(""); 
        } 
        return true; // i.e. we handled it 
       } 
       return false; // i.e. the system should handle it 
      } 
      }); 

     // Alternative: also have row.xml layout with just one text field. No ViewBinder 
     // needed for that simpler approach. 

     setListAdapter(mCursorAdapter); 
     registerForContextMenu(getListView()); 

     // Placing a clickable control inside a list is nontrivial unfortunately. 
     // see bug: http://code.google.com/p/android/issues/detail?id=3414 

    } 

    // Placing this next to onCreate(), help to remember to mDB.close(). 
    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     mDB.close(); 
    } 

    // Create menu when the select the menu button. 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.pref_menu, menu); 
     return true; 
    } 

    // Called for menu item select. Return true if we handled it. 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle item selection 
     switch (item.getItemId()) { 
      case R.id.prefs: 
       // open prefs, previous lecture 
       return true; 

      default: 
       return super.onOptionsItemSelected(item); 

     } 
    } 


    // Create context menu for click-hold in list. 
    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
            ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, v, menuInfo); 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.list_menu, menu); 
    } 

    // Context menu item-select. 
    @Override 
    public boolean onContextItemSelected(MenuItem item) { 
     AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 
     switch (item.getItemId()) { 
      case R.id.menu_detail: 
       startDetail(info.id, false); 
       return true; 

      case R.id.menu_delete: 
       remove(info.id); 
       return true;     
      default: 
       return super.onContextItemSelected(item); 
     } 
    } 


    // Removes the given rowId from the database, updates the UI. 
    public void remove(long rowId) { 
     mDB.deleteRow(rowId); 
     //mCursorAdapter.notifyDataSetChanged(); // confusingly, this does not work 
     mCursorAdapter.getCursor().requery(); // need this 
    } 


    public static final String EXTRA_ROWID = "rowid"; 

    @Override 
    protected void onListItemClick(ListView l, View v, int position, long rowId) { 
     super.onListItemClick(l, v, position, rowId); 
     startDetail(rowId, false); 
    } 

    // Starts the detail activity, either edit existing or create new. 
    public void startDetail(long rowId, boolean create) { 
     Intent intent = new Intent(this, DetailActivity.class); 
     // Our convention: add rowId to edit existing. To create add nothing. 
     if (!create) { 
      intent.putExtra(EXTRA_ROWID, rowId); 
     } 
     startActivity(intent); 
     // Easy bug: remember to add to add a manifest entry for the detail activity 
    } 
} 


/* 
Customizing how the data goes into each list/row (use with row2 layout) 

     mCursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { 
      public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 
       if (columnIndex == RaceDB.INDEX_STATE) { 
        TextView tv = (TextView) view; 
        if (cursor.getInt(RaceDB.INDEX_STATE) > 0) { 
         tv.setText(" (done) "); 
        } 
        else { 
         tv.setText(""); 
        } 
        return true; 
       } 
       return false; 
      } 
      }); 
*/ 

logcat的

12-02 22:37:47.785: D/INFORMATION(768): Database is now open and saved 
12-02 22:37:47.785: D/INFORMATION(768): Database is now able to be shown 
12-02 22:38:24.275: D/INFORMATION(768): Database is now almost shown 
12-02 22:38:24.310: D/AndroidRuntime(768): Shutting down VM 
12-02 22:38:24.310: W/dalvikvm(768): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
12-02 22:38:24.375: E/AndroidRuntime(768): FATAL EXCEPTION: main 
12-02 22:38:24.375: E/AndroidRuntime(768): java.lang.IllegalStateException: Could not execute method of the activity 
12-02 22:38:24.375: E/AndroidRuntime(768): at android.view.View$1.onClick(View.java:2144) 
12-02 22:38:24.375: E/AndroidRuntime(768): at android.view.View.performClick(View.java:2485) 
12-02 22:38:24.375: E/AndroidRuntime(768): at android.view.View$PerformClick.run(View.java:9080) 
12-02 22:38:24.375: E/AndroidRuntime(768): at android.os.Handler.handleCallback(Handler.java:587) 
12-02 22:38:24.375: E/AndroidRuntime(768): at android.os.Handler.dispatchMessage(Handler.java:92) 
12-02 22:38:24.375: E/AndroidRuntime(768): at android.os.Looper.loop(Looper.java:123) 
12-02 22:38:24.375: E/AndroidRuntime(768): at android.app.ActivityThread.main(ActivityThread.java:3683) 
12-02 22:38:24.375: E/AndroidRuntime(768): at java.lang.reflect.Method.invokeNative(Native Method) 
12-02 22:38:24.375: E/AndroidRuntime(768): at java.lang.reflect.Method.invoke(Method.java:507) 
12-02 22:38:24.375: E/AndroidRuntime(768): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
12-02 22:38:24.375: E/AndroidRuntime(768): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
12-02 22:38:24.375: E/AndroidRuntime(768): at dalvik.system.NativeStart.main(Native Method) 
12-02 22:38:24.375: E/AndroidRuntime(768): Caused by: java.lang.reflect.InvocationTargetException 
12-02 22:38:24.375: E/AndroidRuntime(768): at java.lang.reflect.Method.invokeNative(Native Method) 
12-02 22:38:24.375: E/AndroidRuntime(768): at java.lang.reflect.Method.invoke(Method.java:507) 
12-02 22:38:24.375: E/AndroidRuntime(768): at android.view.View$1.onClick(View.java:2139) 
12-02 22:38:24.375: E/AndroidRuntime(768): ... 11 more 
12-02 22:38:24.375: E/AndroidRuntime(768): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {edu.CIS2818.TriTracker/edu.CIS2818.TriTracker.RaceListActivity}; have you declared this activity in your AndroidManifest.xml? 
12-02 22:38:24.375: E/AndroidRuntime(768): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405) 
12-02 22:38:24.375: E/AndroidRuntime(768): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379) 
12-02 22:38:24.375: E/AndroidRuntime(768): at android.app.Activity.startActivityForResult(Activity.java:2827) 
12-02 22:38:24.375: E/AndroidRuntime(768): at android.app.Activity.startActivity(Activity.java:2933) 
12-02 22:38:24.375: E/AndroidRuntime(768): at edu.CIS2818.TriTracker.DetailActivity.view_races(DetailActivity.java:62) 
12-02 22:38:24.375: E/AndroidRuntime(768): ... 14 more 
</pre> 
+0

你是否在清单文件中声明了RaceListActivity.java? –

回答

1

钍错误电子事业低于:

Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class 

有你<application>标签内声明的清单您的活动?

<activity android:name=".RaceListActivity" android:label="@string/app_name"></activity> 
2

请勿手动调用活动生命周期方法。如在您当前的代码中,您正在调用onPause();以及Activity的onCreate。所以改变你的代码:

mDB = new RaceDB(this); 
    mDB.open(); 
    Log.d(TAG, "Database is now open and saved"); 
    dbToUI(); 
    Log.d(TAG, "Database is now able to be shown"); 
    /// onPause(); remove this method from here this 
        //will called automatically by system 

,并确保您已声明RaceListActivity活动中表现为:

<activity android:name=".RaceListActivity" /> 
+0

谢谢。我忘记了清单文件。伊姆兰 - 如果我不放在onPause Logcat给我一个“无法暂停活动”的错误。任何线索? –

+0

最受欢迎的朋友!!! :) –

+0

但你为什么要在'onCreate'内调用'onPause'。为什么你可以调用save();而不是'onCreate'方法中的'onPause' –

2

您必须添加RaceListActivity项在您的项目清单文件:

<activity 
     android:name=".RaceListActivity" 
     android:label="@string/app_name" > 

    </activity> 
相关问题