2012-02-13 43 views
0

我目前正在通过Java Android书籍工作并遇到问题。我正在创建一个基本的数独应用程序,并试图添加一个OnClickListener,这样当有人点击“关于”按钮时,它会显示文本,只要按下此按钮,我就会收到如下错误消息。 '应用程序Sudoku已停止,请重试。Android OnClick错误

这里是我的代码

清单文件

<?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); 
    } 

} 

,最后是我的独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) 
+1

编辑你的问题,用'Exception'(红色)添加从the'Logcat'行。如果在Logcat中没有看到任何内容,请转至Logcat,然后在左侧单击“All messages(no filter)”。 – Luksprog 2012-02-13 20:56:47

+0

在您的清单中,您可能存在内部活动的问题。您的标签关于数独。 – 2012-02-13 21:02:28

回答

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> 
+0

我尝试了上面的内容,添加了logCat中的错误,我只是在清单文件中使用了布局。谢谢 – mitchnufc 2012-02-13 21:02:39

+0

从错误消息看来,系统似乎找不到活动声明。清单更改应该有所帮助。 – Selkie 2012-02-13 21:08:15

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> 

这应该有效。

+0

感谢你们,虽然我很感谢你的回答,但你能否向我解释我的原始解决方案有什么问题,因为这是它在书中给你的一个问题,而且我正在通过我不想要的书如果您知道我的意思,请跳过此步骤 – mitchnufc 2012-02-13 21:05:27

+0

您的Manifest.xml已损坏。您在SudokuActivity中定义了“关于”活动。将findViewById(资源)的结果转换为您期望的类也是很好的选择。如果你不投你的结果,你正在处理一个View对象,它不知道Buttons,Textviews等方法。 – 2012-02-13 21:08:04

+0

啊对,这很有道理该死的书,显然这需要自己的活动。我在哪里感谢你的帮助。 – mitchnufc 2012-02-13 21:10:15

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> 
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>