Android的错误的OnClick [英] Android OnClick error
问题描述
我目前通过Java的Android书工作,都创下了问题。我创建一个基本的数独的应用程序,我试图添加一个OnClickListener,这样,当有人点击关于按钮,它会显示文字,每当这个按钮被击中我刚刚得到的错误信息如下。 数独已停止该应用程序,请重试。
下面是我的code
清单文件
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=org.example.sudoku
安卓版code =1
机器人:=的versionName1.0> <采用-SDK安卓的minSdkVersion =8/> <应用
机器人:图标=@绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME>
<活动
机器人:名字=数独
机器人:标签=@字符串/ APP_NAME> <活动
机器人:关于NAME =
机器人:标签=@字符串/ about_title>
< /活性GT; &所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT; < /用途>< /清单>
我有一个有关类
包org.example.sudoku;进口android.app.Activity;
进口android.os.Bundle;公共类关于延伸活动{
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.about);
}}
,最后是我的独Java类
包org.example.sudoku;进口android.app.Activity;
进口android.os.Bundle;
进口android.view.View;
进口android.content.Intent;
进口android.view.View.OnClickListener;公共类数独扩展活动实现OnClickListener {
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); //设置了按键监听
查看continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(本);
查看newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(本);
查看aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(本);
查看exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(本); } 公共无效的onClick(视图v){
开关(v.getId()){
案例R.id.about_button:
意图I =新意图(这一点,About.class);
startActivity(ⅰ);
打破;
} }
}
感谢看着这个和我使用的Eclipse的方式,使用logcat的和去开溜,但没有运气tryed。
LogCat中的错误
20 02-13:33:08.376:E / AndroidRuntime(524):致命异常:主要
02-13 20:33:08.376:E / AndroidRuntime(524):android.content.ActivityNotFoundException:无法找到明确的活动类{org.example.sudoku / org.example.sudoku.About};有你宣布你的AndroidManifest.xml这个活动?
02-13 20:33:08.376:E / AndroidRuntime(524):在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
02-13 20:33:08.376:E / AndroidRuntime(524):在android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
02-13 20:33:08.376:E / AndroidRuntime(524):在android.app.Activity.startActivityForResult(Activity.java:3190)
02-13 20:33:08.376:E / AndroidRuntime(524):在android.app.Activity.startActivity(Activity.java:3297)
02-13 20:33:08.376:E / AndroidRuntime(524):在org.example.sudoku.Sudoku.onClick(Sudoku.java:34)
02-13 20:33:08.376:E / AndroidRuntime(524):在android.view.View.performClick(View.java:3511)
02-13 20:33:08.376:E / AndroidRuntime(524):在android.view.View $ PerformClick.run(View.java:14105)
02-13 20:33:08.376:E / AndroidRuntime(524):在android.os.Handler.handleCallback(Handler.java:605)
02-13 20:33:08.376:E / AndroidRuntime(524):在android.os.Handler.dispatchMessage(Handler.java:92)
02-13 20:33:08.376:E / AndroidRuntime(524):在android.os.Looper.loop(Looper.java:137)
02-13 20:33:08.376:E / AndroidRuntime(524):在android.app.ActivityThread.main(ActivityThread.java:4424)
02-13 20:33:08.376:E / AndroidRuntime(524):在java.lang.reflect.Method.invokeNative(本机方法)
02-13 20:33:08.376:E / AndroidRuntime(524):在java.lang.reflect.Method.invoke(Method.java:511)
02-13 20:33:08.376:E / AndroidRuntime(524):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784)
02-13 20:33:08.376:E / AndroidRuntime(524):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-13 20:33:08.376:E / AndroidRuntime(524):在dalvik.system.NativeStart.main(本机方法)
您已经定义的活动不对清单中。它的一部分应该是如下:
<应用
机器人:图标=@绘制/ ic_launcher
机器人:标签=@字符串/ APP_NAME>
<活动
机器人:名字=数独
机器人:标签=@字符串/ APP_NAME>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT; <活动
机器人:关于NAME =
机器人:标签=@字符串/ about_title>
< /活性GT; < /用途>
I am currently working through a Java Android book and have hit a problem. I am creating a basic sudoku app and am trying to add a OnClickListener so that when someone hits the 'About' button it displays text, whenever this button is hit I just get the error message as follows. 'The application Sudoku has stopped, please try again.
Here is my code
Manifest file
<?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>
I have an about class
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);
}
}
and lastly is my Sudoku java class
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;
}
}
}
Thanks for looking at this and I'm using eclipse by the way, tryed using the Logcat and De-Bugger but no luck.
LogCat errors
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)
you have defined activity wrong in manifest. A part of it should be as below:
<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>
这篇关于Android的错误的OnClick的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!