- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试在 eclipse java android 中编写代码以将两个字段输入到数据库中。在数据库中输入 3 行条目后,我单击“查看列表”数据库,但出现错误。我已经将我认为的错误缩小到 DetailActivity.java
Intent intent = new Intent(this, RaceListActivity.class);
存在 RaceListActivity.class
和 RaceListActivity.java.
但我收到此错误。 DetailActivity.java
和 RaceListActivity.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>
最佳答案
不要手动调用 Activity 生命周期方法。在您当前的代码中,您正在调用 onPause();
和 onCreate
的 Activity。所以将您的代码更改为:
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
并确保您已在 Manifest 中将 RaceListActivity
Activity 声明为:
<activity android:name=".RaceListActivity" />
关于android - 非法状态异常 : Could not execute method of the activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13677173/
我对 Android 很陌生,如果问题重复,请避免并发送链接。有三个 Activity A、B 和 C。 Activity A 获取一个用户名,我想在 Activity C 中显示该用户名,但我想先运
我正在尝试制作记事本应用程序,因此每次打开新笔记时,布局都会相同。另外, Activity 的数量(新注释)不应定义得尽可能多 最佳答案 如果 Activity 始终相同,您可能应该创建一个适配器,允
我有 3 个 Activity 。 主窗口 5 个按钮 在按钮的主窗口中按下此窗口打开(将其称为父窗口) 在父窗口按钮上按下此窗口打开调用它作为结束子窗口。 现在从子窗口我从父窗口获取值如下:
我遇到了一个 Activity backstack 问题。假设我的后台有 5 个 Activity :比如 Activity A、 Activity B、 Activity C、 Activity D
我正在寻找必须具有以下附加特征的 JMS 提供程序: 采用多代理,所有代理都必须处于事件状态(无单点故障) 仅在两台机器上进行扩展就足以满足我们的需求 能够保证订购(如果 1 个生产者 + 1 个消费
假设,我有一个由 TabHost 组成的选项卡 Activity 。 TabHost 包含 2 个选项卡,每两个选项卡都有一个 Activity 组。每个 Activity 组包含一项 Activit
我正在开发一个应用程序,我需要根据某些操作导航到特定 Activity 。这是一张图片 我的第一个 Activity 是 ReadingActivity。基于某些操作,用户将被带到 NewProjec
我创建了一个与服务器异步通信的应用程序。当应用程序发出服务器请求时,将创建一个带有“正在加载”通知的新对话框( Activity )。主要 Activity 实现了处理服务器响应的方法,我想在主要 A
我想在我的所有应用程序 Activity 中显示相同的选项菜单。我创建了一个实现菜单的通用 Activity ,并且我所有的进一步 Activity 都扩展了它。 问题:当我需要扩展其他特定 Acti
我有四个 Activity ,即 java 文件 - Activity1.java、activity2.java、activity3.java、activity4.java 和 xml 文件 - Ac
我有两个 Activity 。我想将数据从第二个 Activity 发送到上一个 Activity 。第一个 Activity 有自定义 ListView 和 bean 类。当我点击第二个 Activ
根 Activity 是堆栈中当前的第一个 Activity 还是 list 中指定为启动 Activity 的 Activity ? 支持应用程序 P 在启动时启动 Activity A。然后 A
你好 我想知道您在绘制 Activity 图选择“Activity ”时考虑了哪些关键点? 您如何从要建模的问题中选择 Activity ? 谢谢 最佳答案 Activity 图用于对正在开发的系统和
如何从主 Activity 启动 Activity 并在子 Activity 返回主 Activity 中退出操作后返回主 Activity ? 我已将子 Activity 作为启动器 Intent
我的工作流程如下: 登录 Activity -> ActivityB -> ActivityC -> ActivityD 我想将数据从LoginActivity传递到ActivityD,但不直接传递到
我之前曾尝试获得此问题的答案,但找不到可以解决我的问题的答案。我正在制作保存圆盘高尔夫球分数的应用程序。我的 MainActivity 有 4 个按钮。新比赛、恢复比赛、类(class)和球员。 At
我有一个 tts 非 UI 类和 Activity 类。现在在 Activity 类中,我有一个按钮,用户可以从中选择男声或女声,具体取决于我想要将字符串传递给 tts 类的选择,然后一次tts 类根
问题有点复杂,首先, Activity A 和 Activity B 的 list 中都有 android:noHistory = true 。我有一个自定义 serialized 类,假设 MyCl
在我的应用程序中,我有两个 Activity (AuthenticationActivity 和 MainActivity),每个 Activity 都有一个导航图和大量 fragment 。我创建了
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: How can i use compose email activity in tabView? 我想在选项
我是一名优秀的程序员,十分优秀!