问题
I'm creating a sudoku game, but when i press the button it crashes the game. Here's my code:
FPSudoku.java:
package org.example.fpsudoku;
import android.app.Activity; import
android.os.Bundle; import
android.content.Intent; import
android.view.View; import
android.view.View.OnClickListener;
public class FPSudoku extends Activity
implements OnClickListener{
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View newButton = findViewById(R.id.new_game_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.how_to_play_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_game_button);
exitButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.how_to_play_button:
Intent i = new Intent(this, Howtoplay.class);
startActivity(i);
break;
}
}
}
Howtoplay.java
package org.example.fpsudoku;
import android.app.Activity; import
android.os.Bundle;
public class Howtoplay extends
Activity { @Override protected void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.howtoplay);
}
}
UPDATED
android manifest.xml
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".FPSudoku"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<activity android:name=".Howtoplay"
android:label="@string/how_to_play_title" >
</activity>
</activity>
</application>
updated logcat
02-25 00:36:08.623: DEBUG/AndroidRuntime(818): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
02-25 00:36:08.623: DEBUG/AndroidRuntime(818): CheckJNI is ON
02-25 00:36:08.763: DEBUG/AndroidRuntime(818): --- registering native functions ---
02-25 00:36:09.504: DEBUG/dalvikvm(237): GC_EXPLICIT freed 130 objects / 6944 bytes in 127ms
02-25 00:36:09.544: DEBUG/PackageParser(43): Scanning package: /data/app/vmdl10845.tmp
02-25 00:36:09.544: WARN/PackageParser(43): Problem in package /data/app/vmdl10845.tmp:
02-25 00:36:09.544: WARN/PackageParser(43): Unknown element under : activity at /data/app/vmdl10845.tmp Binary XML file line #15
02-25 00:36:09.634: INFO/PackageManager(43): Removing non-system package:org.example.fpsudoku
02-25 00:36:09.634: INFO/ActivityManager(43): Force stopping package org.example.fpsudoku uid=10036
02-25 00:36:09.734: DEBUG/PackageManager(43): Scanning package org.example.fpsudoku
02-25 00:36:09.734: INFO/PackageManager(43): Package org.example.fpsudoku codePath changed from /data/app/org.example.fpsudoku-1.apk to /data/app/org.example.fpsudoku-2.apk; Retaining data and using new
02-25 00:36:09.744: INFO/PackageManager(43): /data/app/org.example.fpsudoku-2.apk changed; unpacking
02-25 00:36:09.754: DEBUG/installd(35): DexInv: --- BEGIN '/data/app/org.example.fpsudoku-2.apk' ---
02-25 00:36:09.904: DEBUG/dalvikvm(825): DexOpt: load 28ms, verify 17ms, opt 2ms
02-25 00:36:09.904: DEBUG/installd(35): DexInv: --- END '/data/app/org.example.fpsudoku-2.apk' (success) ---
02-25 00:36:09.914: WARN/PackageManager(43): Code path for pkg : org.example.fpsudoku changing from /data/app/org.example.fpsudoku-1.apk to /data/app/org.example.fpsudoku-2.apk
02-25 00:36:09.914: WARN/PackageManager(43): Resource path for pkg : org.example.fpsudoku changing from /data/app/org.example.fpsudoku-1.apk to /data/app/org.example.fpsudoku-2.apk
02-25 00:36:09.914: DEBUG/PackageManager(43): Activities: org.example.fpsudoku.FPSudoku
02-25 00:36:09.924: INFO/ActivityManager(43): Force stopping package org.example.fpsudoku uid=10036
02-25 00:36:10.004: INFO/installd(35): move /data/dalvik-cache/data@app@org.example.fpsudoku-2.apk@classes.dex -> /data/dalvik-cache/data@app@org.example.fpsudoku-2.apk@classes.dex
02-25 00:36:10.014: DEBUG/PackageManager(43): New package installed in /data/app/org.example.fpsudoku-2.apk
02-25 00:36:10.174: DEBUG/dalvikvm(43): GC_FOR_MALLOC freed 7382 objects / 491576 bytes in 97ms
02-25 00:36:10.274: INFO/ActivityManager(43): Force stopping package org.example.fpsudoku uid=10036
02-25 00:36:10.334: DEBUG/dalvikvm(117): GC_EXPLICIT freed 814 objects / 43456 bytes in 53ms
02-25 00:36:10.554: DEBUG/dalvikvm(43): GC_EXPLICIT freed 3804 objects / 210760 bytes in 102ms
02-25 00:36:10.574: WARN/RecognitionManagerService(43): no available voice recognition services found
02-25 00:36:10.614: INFO/installd(35): unlink /data/dalvik-cache/data@app@org.example.fpsudoku-1.apk@classes.dex
02-25 00:36:10.654: DEBUG/AndroidRuntime(818): Shutting down VM
02-25 00:36:10.664: DEBUG/dalvikvm(818): Debugger has detached; object registry had 1 entries
02-25 00:36:10.694: INFO/AndroidRuntime(818): NOTE: attach of thread 'Binder Thread #3' failed
02-25 00:36:11.254: DEBUG/AndroidRuntime(831): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
02-25 00:36:11.254: DEBUG/AndroidRuntime(831): CheckJNI is ON
02-25 00:36:11.414: DEBUG/AndroidRuntime(831): --- registering native functions ---
02-25 00:36:12.034: INFO/ActivityManager(43): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=org.example.fpsudoku/.FPSudoku }
02-25 00:36:12.064: INFO/ActivityManager(43): Start proc org.example.fpsudoku for activity org.example.fpsudoku/.FPSudoku: pid=837 uid=10036 gids={}
02-25 00:36:12.094: DEBUG/AndroidRuntime(831): Shutting down VM
02-25 00:36:12.104: DEBUG/dalvikvm(831): Debugger has detached; object registry had 1 entries
02-25 00:36:12.954: INFO/ActivityManager(43): Displayed activity org.example.fpsudoku/.FPSudoku: 901 ms (total 901 ms)
02-25 00:36:14.024: INFO/ActivityManager(43): Starting activity: Intent { cmp=org.example.fpsudoku/.Howtoplay }
02-25 00:36:14.034: DEBUG/AndroidRuntime(837): Shutting down VM
02-25 00:36:14.034: WARN/dalvikvm(837): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-25 00:36:14.044: ERROR/AndroidRuntime(837): FATAL EXCEPTION: main
02-25 00:36:14.044: ERROR/AndroidRuntime(837): android.content.ActivityNotFoundException: Unable to find explicit activity class {org.example.fpsudoku/org.example.fpsudoku.Howtoplay}; have you declared this activity in your AndroidManifest.xml?
02-25 00:36:14.044: ERROR/AndroidRuntime(837): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
02-25 00:36:14.044: ERROR/AndroidRuntime(837): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
02-25 00:36:14.044: ERROR/AndroidRuntime(837): at android.app.Activity.startActivityForResult(Activity.java:2817)
02-25 00:36:14.044: ERROR/AndroidRuntime(837): at android.app.Activity.startActivity(Activity.java:2923)
02-25 00:36:14.044: ERROR/AndroidRuntime(837): at org.example.fpsudoku.FPSudoku.onClick(FPSudoku.java:31)
02-25 00:36:14.044: ERROR/AndroidRuntime(837): at android.view.View.performClick(View.java:2408)
02-25 00:36:14.044: ERROR/AndroidRuntime(837): at android.view.View$PerformClick.run(View.java:8816)
02-25 00:36:14.044: ERROR/AndroidRuntime(837): at android.os.Handler.handleCallback(Handler.java:587)
02-25 00:36:14.044: ERROR/AndroidRuntime(837): at android.os.Handler.dispatchMessage(Handler.java:92)
02-25 00:36:14.044: ERROR/AndroidRuntime(837): at android.os.Looper.loop(Looper.java:123)
02-25 00:36:14.044: ERROR/AndroidRuntime(837): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-25 00:36:14.044: ERROR/AndroidRuntime(837): at java.lang.reflect.Method.invokeNative(Native Method)
02-25 00:36:14.044: ERROR/AndroidRuntime(837): at java.lang.reflect.Method.invoke(Method.java:521)
02-25 00:36:14.044: ERROR/AndroidRuntime(837): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-25 00:36:14.044: ERROR/AndroidRuntime(837): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-25 00:36:14.044: ERROR/AndroidRuntime(837): at dalvik.system.NativeStart.main(Native Method)
02-25 00:36:14.054: WARN/ActivityManager(43): Force finishing activity org.example.fpsudoku/.FPSudoku
02-25 00:36:14.564: WARN/ActivityManager(43): Activity pause timeout for HistoryRecord{43f8d620 org.example.fpsudoku/.FPSudoku}
02-25 00:36:15.764: INFO/Process(837): Sending signal. PID: 837 SIG: 9
02-25 00:36:15.774: INFO/ActivityManager(43): Process org.example.fpsudoku (pid 837) has died.
02-25 00:36:15.774: INFO/WindowManager(43): WIN DEATH: Window{440296f8 org.example.fpsudoku/org.example.fpsudoku.FPSudoku paused=false}
02-25 00:36:15.824: WARN/InputManagerService(43): Got RemoteException sending setActive(false) notification to pid 837 uid 10036
02-25 00:36:18.894: DEBUG/SntpClient(43): request time failed: java.net.SocketException: Address family not supported by protocol
02-25 00:36:20.194: DEBUG/dalvikvm(237): GC_EXPLICIT freed 156 objects / 11312 bytes in 133ms
02-25 00:36:24.782: WARN/ActivityManager(43): Activity destroy timeout for HistoryRecord{43f8d620 org.example.fpsudoku/.FPSudoku}
02-25 00:36:25.254: DEBUG/dalvikvm(262): GC_EXPLICIT freed 26 objects / 1144 bytes in 131ms
回答1:
Hidden in your LogCat is the answer to your question:
ActivityNotFoundException: Unable to find explicit activity class {org.example.fpsudoku/org.example.fpsudoku.Howtoplay}; have you declared this activity in your AndroidManifest.xml?
来源:https://stackoverflow.com/questions/5111917/android-sudoku-game-button-press-crashes-the-game