我希望在点击按钮时引导活动。但是我的应用程序崩溃了。
这是我的代码,其中指出了错误(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);
}
});
按钮的xml
<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" />
这是一个名为
的活动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
答案 0 :(得分:3)
将此添加到清单文件...
<activity android:name=".Database" />
答案 1 :(得分:0)
你已经在Button小部件中设置了onclick =“grammer”属性,但在你的MainActivity中没有语法方法。可能你应该这样使用。
public void grammmer(View view)
{
// To do your stuff
}
或
就像你将onclicklistener设置为
一样 Button b1 = (Button)findViewById(R.id.YourButton);
b1.setonClickListener(new View.onClickListener(){
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Database.class);
startActivity(intent);
}
});
和btw,在AndroidManifest.xml中注册您的下一个活动
<activity name=".YourActivity" />
答案 2 :(得分:0)
您没有将活动添加到清单中 - 这意味着您的应用无法识别您的活动。
必须在清单文件中声明所有活动, AndroidManifest.xml,使用
<activity>
元素。
请详细了解,重要的是要知道:
http://developer.android.com/training/basics/firstapp/starting-activity.html
http://developer.android.com/guide/topics/manifest/activity-element.html
例如:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="your.package.name">
<uses-sdk android:minSdkVersion="18" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Activity1" android:label="@string/app_name"></activity>
<activity android:name=".Activity2"></activity>
</application>
</manifest>
答案 3 :(得分:0)
我认为问题可能出于以下问题......
1)在您设置Database.java
的{{1}}中,您的班级名称为数据库,因此请确保您的XML名称正确无误。因为我认为你也有setContentView(R.layout.activity_main);
所以要确保你有两个不同的XML用于这两个活动。并且您将正确的XML分配给正确的Java文件。
2)我认为你没有在main_activity.java
文件中设置数据库名称,所以@Anil Bhatiya告诉你,这样做。
答案 4 :(得分:-2)
您已在xml中设置了android:onClick=grammar
,因此您无需实现onclick侦听器。只需将方法名称创建为“语法”,并将您的意图放在该方法中