- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试运行一个应用程序,但当我选择一个菜单项时,该应用程序立即停止工作,并且出现以下错误:
logcat:
03-23 19:54:11.029: E/AndroidRuntime(1316): FATAL EXCEPTION: main
03-23 19:54:11.029: E/AndroidRuntime(1316): Process: com.example.testcalnd2, PID: 1316
03-23 19:54:11.029: E/AndroidRuntime(1316): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.testcalnd2/com.example.testcalnd2.ActivityDiaryEdit}: java.lang.IllegalAccessException: class com.example.testcalnd2.ActivityDiaryEdit is not accessible from class android.app.Instrumentation
03-23 19:54:11.029: E/AndroidRuntime(1316): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236)
03-23 19:54:11.029: E/AndroidRuntime(1316): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
03-23 19:54:11.029: E/AndroidRuntime(1316): at android.app.ActivityThread.access$800(ActivityThread.java:151)
03-23 19:54:11.029: E/AndroidRuntime(1316): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
03-23 19:54:11.029: E/AndroidRuntime(1316): at android.os.Handler.dispatchMessage(Handler.java:102)
03-23 19:54:11.029: E/AndroidRuntime(1316): at android.os.Looper.loop(Looper.java:135)
03-23 19:54:11.029: E/AndroidRuntime(1316): at android.app.ActivityThread.main(ActivityThread.java:5257)
03-23 19:54:11.029: E/AndroidRuntime(1316): at java.lang.reflect.Method.invoke(Native Method)
03-23 19:54:11.029: E/AndroidRuntime(1316): at java.lang.reflect.Method.invoke(Method.java:372)
03-23 19:54:11.029: E/AndroidRuntime(1316): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
03-23 19:54:11.029: E/AndroidRuntime(1316): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
03-23 19:54:11.029: E/AndroidRuntime(1316): Caused by: java.lang.IllegalAccessException: class com.example.testcalnd2.ActivityDiaryEdit is not accessible from class android.app.Instrumentation
03-23 19:54:11.029: E/AndroidRuntime(1316): at java.lang.Class.newInstance(Class.java:1591)
03-23 19:54:11.029: E/AndroidRuntime(1316): at android.app.Instrumentation.newActivity(Instrumentation.java:1066)
03-23 19:54:11.029: E/AndroidRuntime(1316): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226)
03-23 19:54:11.029: E/AndroidRuntime(1316): ... 10 more
03-23 19:54:20.399: I/Process(1316): Sending signal. PID: 1316 SIG: 9
MainActivity 类:
package com.example.testcalnd2;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
public class MainActivity extends ListActivity {
private static final int ACTIVITY_CREATE = 0;
private static final int ACTIVITY_EDIT = 1;
private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;
private DiaryDbAdapter mDbHelper;
private Cursor mDiaryCursor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.diary_list);
mDbHelper = new DiaryDbAdapter(this);
mDbHelper.open();
renderListView();
}
private void renderListView() {
mDiaryCursor = mDbHelper.getAllNotes();
startManagingCursor(mDiaryCursor);
String[] from = new String[] { DiaryDbAdapter.KEY_TITLE,
DiaryDbAdapter.KEY_CREATED };
int[] to = new int[] { R.id.text1, R.id.created };
SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.diary_now, mDiaryCursor, from, to);
setListAdapter(notes);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, INSERT_ID, 0, R.string.menu_insert);
menu.add(0, DELETE_ID, 0, R.string.menu_delete);
return true;
}
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch (item.getItemId()) {
case INSERT_ID:
createDiary();
return true;
case DELETE_ID:
mDbHelper.deleteDiary(getListView().getSelectedItemId());
renderListView();
return true;
}
return super.onMenuItemSelected(featureId, item);
}
private void createDiary() {
Intent i = new Intent(this, ActivityDiaryEdit.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Cursor c = mDiaryCursor;
c.moveToPosition(position);
Intent i = new Intent(this, ActivityDiaryEdit.class);
i.putExtra(DiaryDbAdapter.KEY_ROWID, id);
i.putExtra(DiaryDbAdapter.KEY_TITLE, c.getString(c
.getColumnIndexOrThrow(DiaryDbAdapter.KEY_TITLE)));
i.putExtra(DiaryDbAdapter.KEY_BODY, c.getString(c
.getColumnIndexOrThrow(DiaryDbAdapter.KEY_BODY)));
startActivityForResult(i, ACTIVITY_EDIT);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
renderListView();
}
}
package com.example.testcalnd2;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
DiaryDbAdapter:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Calendar;
import java.util.Date;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
class DiaryDbAdapter {
public static final String KEY_TITLE = "title";
public static final String KEY_BODY = "body";
public static final String KEY_ROWID = "_id";
public static final String KEY_CREATED = "created";
private static final String TAG = "DiaryDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE = "create table diary (_id integer primary key autoincrement, "
+ "title text not null, body text not null, created text not null);";
private static final String DATABASE_NAME = "database";
private static final String DATABASE_TABLE = "diary";
private static final int DATABASE_VERSION = 1;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS diary");
onCreate(db);
}
}
public DiaryDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public DiaryDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void closeclose() {
mDbHelper.close();
}
public long createDiary(String title, String body) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_BODY, body);
Calendar calendar = Calendar.getInstance();
String created = calendar.get(Calendar.YEAR) + ""
+ calendar.get(Calendar.MONTH) + ""
+ calendar.get(Calendar.DAY_OF_MONTH) + ""
+ calendar.get(Calendar.HOUR_OF_DAY) + ""
+ calendar.get(Calendar.MINUTE) + "";
initialValues.put(KEY_CREATED, created);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deleteDiary(long rowId) {
return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
public Cursor getAllNotes() {
return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_TITLE,
KEY_BODY, KEY_CREATED }, null, null, null, null, null);
}
public Cursor getDiary(long rowId) throws SQLException {
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] { KEY_ROWID, KEY_TITLE,
KEY_BODY, KEY_CREATED }, KEY_ROWID + "=" + rowId, null, null,
null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateDiary(long rowId, String title, String body) {
ContentValues args = new ContentValues();
args.put(KEY_TITLE, title);
args.put(KEY_BODY, body);
Calendar calendar = Calendar.getInstance();
String created = calendar.get(Calendar.YEAR) + ""
+ calendar.get(Calendar.MONTH) + ""
+ calendar.get(Calendar.DAY_OF_MONTH) + ""
+ calendar.get(Calendar.HOUR_OF_DAY) + ""
+ calendar.get(Calendar.MINUTE) + "";
args.put(KEY_CREATED, created);
return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}
ActivityDiaryEdit Activity :
class ActivityDiaryEdit extends Activity {
private EditText mTitleText;
private EditText mBodyText;
private Long mRowId;
private DiaryDbAdapter mDbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbHelper = new DiaryDbAdapter(this);
mDbHelper.open();
setContentView(R.layout.diary_edit);
mTitleText = (EditText) findViewById(R.id.title);
mBodyText = (EditText) findViewById(R.id.body);
Button confirmButton = (Button) findViewById(R.id.confirm);
mRowId = null;
Bundle extras = getIntent().getExtras();
if (extras != null) {
String title = extras.getString(DiaryDbAdapter.KEY_TITLE);
String body = extras.getString(DiaryDbAdapter.KEY_BODY);
mRowId = extras.getLong(DiaryDbAdapter.KEY_ROWID);
if (title != null) {
mTitleText.setText(title);
}
if (body != null) {
mBodyText.setText(body);
}
}
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String title = mTitleText.getText().toString();
String body = mBodyText.getText().toString();
if (mRowId != null) {
mDbHelper.updateDiary(mRowId, title, body);
} else
mDbHelper.createDiary(title, body);
Intent mIntent = new Intent();
setResult(RESULT_OK, mIntent);
finish();
}
});
}
}
list :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testcalnd2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DiaryDbAdapter"
android:label="@string/title_activity_diary_db_adapter" >
</activity>
<activity
android:name="com.example.testcalnd2.ActivityDiaryEdit">
</activity>
</application>
</manifest>
最佳答案
像这样公开你的类ActivityDiaryEdit
:
public class ActivityDiaryEdit extends Activity {
...
希望这对您有所帮助!
关于android - java.lang.RuntimeException : Unable to instantiate activity ComponentInfo/is not accessible from class 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36187928/
我正在尝试为 Android 设置 JUnit 测试并且我有 list 等设置但我不断收到此错误: java.lang.RuntimeException: Unable to instantiate
我的问题是当我运行应用程序并单击注册按钮时突然显示弹出窗口:“强制关闭” 这是我的代码: Main.java package com.example.server; import java.util.
当我运行我的 Activity registeration.java 或 login.java 时,它将 Intent 中的用户名传递给名为 profile.java 的新 Activity > 关于
这个问题已经有答案了: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo (47 个回答) 已关闭 8
以下 Android Studio 1.5 MainActivity.java 代码会在标题中生成错误,并带有 You need to use a Theme.AppCompat theme publ
尝试从文本消息 URL 在 Android 上启动 Xamarin 表单应用程序时,我收到此错误。我一直在遵循 THIS 中提到的步骤文章。 这里是我 AppManifest.xml 中的应用程序节点
我在我的应用程序中创建了一个 map View ,但出现了这个错误: 09-07 21:24:08.886: INFO/ActivityManager(243): Starting: Intent {
我的应用程序运行正常,但当应用程序进入后台时,它在应该恢复时崩溃了。正如您在源代码中看到的那样,我记录了 onStart、onStop 等事件。 在我的日志中,当我启动应用程序时,我可以看到 onSt
我正在处理从其他人那里导入的 Android 项目。我已经对所有依赖项进行排序,项目中没有错误,但是当我尝试启动它时,我得到: 04-08 16:49:41.761: E/AndroidRuntime
在我的应用程序中,当我选择按钮时,它会将我指向 mapView 类,它会显示用户的当前位置。 但是,它抛出 java.lang.RuntimeException: Unable to instanti
我在开发 Android 应用程序时遇到问题。我首先对我的错误进行了研究,然后发现还有其他人和我有同样的问题。我阅读了所有评论并尝试了所有方法,但我仍然遇到同样的错误。 这里是我的错误 05-29 1
我有一个 Google 云消息的接收器,它已在 AndroidManifest.xml 中注册:
我正在开发一个应用程序,最低版本为 Froyo,目标版本为 Gingerbread。因此, list 显示: 我有一个模拟器和一个带有 Gingerbread 的 Nexus One,应用程序部署和
我正在尝试将其作为我的主要 Activity 以打开另一个 Activity (ListAtivity)。但是,当我单击按钮时,应用程序崩溃并出现以下异常: 01-26 17:12:58.341: E
我正在制作一个可以搜索表“员工”并返回结果的应用程序。我正在使用 searchdialog 进行搜索。但是我在搜索对话框上按下搜索按钮时得到了 FC。请帮帮我。 代码: public class Si
我是 Android 开发的新手,所以我确信这会变成我错过的愚蠢的事情,但我有两个 Activity :StartScreen 和 Map。 StartScreen 只是简单的文本和一个按钮,然后应该
我修改了来自 http://www.elektor.com/magazines/2012/march/android-switch-interface.2084156.lynkx 的示例蓝牙通信应用程
我创建了一个应用程序,该应用程序应该从我的Thingspeak channel 接收数据。 首先,我只是使用了webview小部件,但我想走得更远,并使用Thingspeak Java api自己处理
可以找到 list 文件here . 可以找到DeviceAdminReceiver类here agent_device_xml 定义如下:
01-05 18:35:42.754: E/AndroidRuntime(5814): java.lang.RuntimeException: 无法实例化 Activity ComponentInfo
我是一名优秀的程序员,十分优秀!