我希望點擊某個按鈕時指引一項活動。但是,當完成這個工作時,我的應用程序崩潰了。onclick上的錯誤
這是我的代碼,其中指出(startActivity(intent);
)錯誤,
Button ExerciseButton = (Button)findViewById(R.id.button3);
ExerciseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Database.class);
startActivity(intent);
}
});
的按鈕
<Button
android:id="@+id/button3"
android:layout_width="110dp"
android:layout_height="50dp"
android:background="@drawable/button"
android:onClick="grammar"
android:text="@string/button_exercise"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:layout_marginLeft="28dp" />
這是被稱爲活性的XML,
public class Database extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
String destPath = "/data/data/" + getPackageName()
+ "/databases/questions";
File f = new File(destPath);
if (!f.exists()) {
CopyDB(getBaseContext().getAssets().open("questions"),
new FileOutputStream(destPath));
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(Database.this, "Error File", Toast.LENGTH_SHORT)
.show();
}
catch (IOException e1) {
e1.printStackTrace();
Toast.makeText(Database.this, "Error IO", Toast.LENGTH_SHORT)
.show();
}
DatabaseHandler db = new DatabaseHandler(this);
long id = db.addQuestion("Question1", "answer1", "answer2", "answer3", "answer4", "answer");
//id = db.addQuestion("Question2", "answer2");
db.close();
Cursor cursor = db.getAllQuestions();
if (cursor.moveToFirst())
{
do {
DisplayRecord(cursor);
}
while(cursor.moveToNext());
}
db.close();
}
public void CopyDB(InputStream inputstream, OutputStream outputstream)
throws IOException {
byte[] buffer = new byte[1024];
int length;
while ((length = inputstream.read(buffer))>0){
outputstream.write(buffer, 0, length);
}
inputstream.close();
outputstream.close();
}
public void DisplayRecord(Cursor cursor){
Toast.makeText(this,
"id:" + cursor.getString(0) + "\n" +
"Question:" + cursor.getString(1) + "\n" +
"Answer1:" + cursor.getString(2) + "\n" +
"Answer2:" + cursor.getString(3) + "\n" +
"Answer3:" + cursor.getString(4) + "\n" +
"Answer4:" + cursor.getString(5) + "\n" +
"Answer:" + cursor.getString(6),
Toast.LENGTH_SHORT).show();
}
}
logcat在下面。
02-27 10:35:41.061: W/KeyCharacterMap(11469): No keyboard for id 0
02-27 10:35:41.061: W/KeyCharacterMap(11469): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
02-27 10:35:42.471: D/AndroidRuntime(11469): Shutting down VM
02-27 10:35:42.481: W/dalvikvm(11469): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-27 10:35:42.491: E/AndroidRuntime(11469): FATAL EXCEPTION: main
02-27 10:35:42.491: E/AndroidRuntime(11469): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.grammer/com.example.grammer.Database}; have you declared this activity in your AndroidManifest.xml?
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1405)
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1379)
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.app.Activity.startActivityForResult(Activity.java:2827)
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.app.Activity.startActivity(Activity.java:2933)
02-27 10:35:42.491: E/AndroidRuntime(11469): at com.example.grammer.MainActivity$2.onClick(MainActivity.java:43)
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.view.View.performClick(View.java:2485)
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.view.View$PerformClick.run(View.java:9080)
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.os.Handler.handleCallback(Handler.java:587)
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.os.Handler.dispatchMessage(Handler.java:92)
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.os.Looper.loop(Looper.java:123)
02-27 10:35:42.491: E/AndroidRuntime(11469): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-27 10:35:42.491: E/AndroidRuntime(11469): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 10:35:42.491: E/AndroidRuntime(11469): at java.lang.reflect.Method.invoke(Method.java:507)
02-27 10:35:42.491: E/AndroidRuntime(11469): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-27 10:35:42.491: E/AndroidRuntime(11469): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-27 10:35:42.491: E/AndroidRuntime(11469): at dalvik.system.NativeStart.main(Native Method)
02-27 10:35:44.961: I/Process(11469): Sending signal. PID: 11469 SIG: 9
後logcat ........... –
你把你的數據庫活動清單?請發佈您的logcat。 – Amrut
在日誌中:'數據庫:你有沒有在你的AndroidManifest.xml中聲明數據庫活動?' –