2

Eclipse java androidでコードを記述して、データベースに2つのフィールドを入力しようとしています。データベースへの3行のエントリの後、[リストの表示]データベースをクリックすると、エラーが発生します。私は私が考えるエラーをに絞り込みましたDetailActivity.java

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

が存在しますが、RaceListActivity.classこのRaceListActivity.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>
4

3 に答える 3

2

アクティビティのライフサイクル メソッドを手動で呼び出さないでください。現在のコードのように、Activityで呼び出しonPause();ています。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" />
于 2012-12-03T04:37:16.417 に答える
2

プロジェクトのマニフェスト ファイルに RaceListActivity エントリを追加する必要があります。

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

    </activity>
于 2012-12-03T04:37:28.637 に答える
1

エラーの原因は以下です。

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

<application>タグ内のマニフェストでアクティビティを宣言しましたか?

<activity android:name=".RaceListActivity" android:label="@string/app_name"></activity>
于 2012-12-03T04:37:04.690 に答える