我目前正在阅读一本Java Android手册并遇到了问题。我正在创建一个基本的数独应用程序,我正在尝试添加一个OnClickListener,这样当有人点击“关于”按钮时,它会显示文本,每当按下此按钮时,我只会收到如下错误消息。 '应用程序数独已经停止,请再试一次。
这是我的代码
清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.sudoku"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".Sudoku"
android:label="@string/app_name" >
<activity
android:name=".About"
android:label="@string/about_title">
</activity>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
我有一个关于课程
package org.example.sudoku;
import android.app.Activity;
import android.os.Bundle;
public class About extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
}
最后是我的Sudoku java类
package org.example.sudoku;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.view.View.OnClickListener;
public class Sudoku extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//set up listeners for buttons
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
}
}
}
感谢您查看此内容,我正在使用eclipse,尝试使用Logcat和De-Bugger,但没有运气。
LogCat错误
02-13 20:33:08.376: E/AndroidRuntime(524): FATAL EXCEPTION: main
02-13 20:33:08.376: E/AndroidRuntime(524): android.content.ActivityNotFoundException: Unable to find explicit activity class {org.example.sudoku/org.example.sudoku.About}; have you declared this activity in your AndroidManifest.xml?
02-13 20:33:08.376: E/AndroidRuntime(524): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
02-13 20:33:08.376: E/AndroidRuntime(524): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
02-13 20:33:08.376: E/AndroidRuntime(524): at android.app.Activity.startActivityForResult(Activity.java:3190)
02-13 20:33:08.376: E/AndroidRuntime(524): at android.app.Activity.startActivity(Activity.java:3297)
02-13 20:33:08.376: E/AndroidRuntime(524): at org.example.sudoku.Sudoku.onClick(Sudoku.java:34)
02-13 20:33:08.376: E/AndroidRuntime(524): at android.view.View.performClick(View.java:3511)
02-13 20:33:08.376: E/AndroidRuntime(524): at android.view.View$PerformClick.run(View.java:14105)
02-13 20:33:08.376: E/AndroidRuntime(524): at android.os.Handler.handleCallback(Handler.java:605)
02-13 20:33:08.376: E/AndroidRuntime(524): at android.os.Handler.dispatchMessage(Handler.java:92)
02-13 20:33:08.376: E/AndroidRuntime(524): at android.os.Looper.loop(Looper.java:137)
02-13 20:33:08.376: E/AndroidRuntime(524): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-13 20:33:08.376: E/AndroidRuntime(524): at java.lang.reflect.Method.invokeNative(Native Method)
02-13 20:33:08.376: E/AndroidRuntime(524): at java.lang.reflect.Method.invoke(Method.java:511)
02-13 20:33:08.376: E/AndroidRuntime(524): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-13 20:33:08.376: E/AndroidRuntime(524): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-13 20:33:08.376: E/AndroidRuntime(524): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
我不确定是不是这个原因,但你的清单文件似乎搞砸了。这两个活动标签混合在一起。您应该将应用程序部分更改为:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".Sudoku"
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=".About"
android:label="@string/about_title">
</activity>
</application>
答案 1 :(得分:1)
您在清单中定义了错误的活动。其中一部分应如下:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".Sudoku"
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=".About"
android:label="@string/about_title">
</activity>
</application>
答案 2 :(得分:0)
Button aboutButton = (Button)findViewById(R.id.about_button);
if (aboutButton != null)
aboutButton.setOnClickListener(this);
else
Log.e("MyTag", "aboutButton not found on View");
还要确保编辑清单。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.sudoku"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".Sudoku"
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=".About"
android:label="@string/about_title">
</activity>
</application>
</manifest>
这应该有用。
答案 3 :(得分:0)
<activity
android:name=".Sudoku"
android:label="@string/app_name" >
<activity
android:name=".About"
android:label="@string/about_title">
</activity>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
应该是:
<activity>
android:name=".Sudoku"
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=".About"
android:label="@string/about_title">
</activity>