Android 应用程序的 java.lang.RuntimeException
我的应用程序遇到问题。编译后不久,应用程序随机关闭。检查LogCat表明它是java.lang.RuntimeException。我已经浏览了所有关于此问题的论坛帖子,并相应地更改了我的代码,但问题仍然存在。帮助?
代码:
public class DroidzActivity extends Activity implements OnGesturePerformedListener {
/** Called when the activity is first created. */
private GestureLibrary mLibrary;
private static final String TAG = MainThread.class.getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MainGamePanel(this));
//requesting to turn the title OFF
//requestWindowFeature(Window.FEATURE_NO_TITLE);
//make it full screen
//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// set the MainGamePanel as the View
Log.d(TAG, "View added");
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!mLibrary.load()) {
finish();
}
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(this);
}
@Override
protected void onDestroy() {
Log.d(TAG, "Destroying...");
super.onDestroy();
}
@Override
protected void onStop() {
Log.d(TAG, "Stopping...");
super.onStop();
}
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
if (predictions.size() > 0) {
if (predictions.get(0).score > 1.0) {
String action = predictions.get(0).name;
if ("action_add".equals(action)) {
Toast.makeText(this, "Adding a contact", Toast.LENGTH_SHORT).show();
} else if ("action_delete".equals(action)) {
Toast.makeText(this, "Removing a contact", Toast.LENGTH_SHORT).show();
} else if ("action_refresh".equals(action)) {
Toast.makeText(this, "Reloading contacts", Toast.LENGTH_SHORT).show();
}
}
}
}
LogCat:
02-08 23:16:54.898: D/MainThread(3027): View added
02-08 23:16:54.937: D/AndroidRuntime(3027): Shutting down VM
02-08 23:16:54.937: W/dalvikvm(3027): threadid=1: thread exiting with uncaught exception (group=0x40015578)
02-08 23:16:54.949: E/AndroidRuntime(3027): FATAL EXCEPTION: main
02-08 23:16:54.949: E/AndroidRuntime(3027): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.obviam.droidz/net.obviam.droidz.DroidzActivity}: java.lang.NullPointerException
02-08 23:16:54.949: E/AndroidRuntime(3027): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
02-08 23:16:54.949: E/AndroidRuntime(3027): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-08 23:16:54.949: E/AndroidRuntime(3027): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-08 23:16:54.949: E/AndroidRuntime(3027): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-08 23:16:54.949: E/AndroidRuntime(3027): at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 23:16:54.949: E/AndroidRuntime(3027): at android.os.Looper.loop(Looper.java:130)
02-08 23:16:54.949: E/AndroidRuntime(3027): at android.app.ActivityThread.main(ActivityThread.java:3687)
02-08 23:16:54.949: E/AndroidRuntime(3027): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 23:16:54.949: E/AndroidRuntime(3027): at java.lang.reflect.Method.invoke(Method.java:507)
02-08 23:16:54.949: E/AndroidRuntime(3027): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
02-08 23:16:54.949: E/AndroidRuntime(3027): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-08 23:16:54.949: E/AndroidRuntime(3027): at dalvik.system.NativeStart.main(Native Method)
02-08 23:16:54.949: E/AndroidRuntime(3027): Caused by: java.lang.NullPointerException
02-08 23:16:54.949: E/AndroidRuntime(3027): at net.obviam.droidz.DroidzActivity.onCreate(DroidzActivity.java:42)
02-08 23:16:54.949: E/AndroidRuntime(3027): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-08 23:16:54.949: E/AndroidRuntime(3027): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
02-08 23:16:54.949: E/AndroidRuntime(3027): ... 11 more
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.obviam.droidz"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:debuggable="true">
<activity
android:name=".DroidzActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
I'm having problems with my application. Shortly after compiling, the application randomly closes. Checking LogCat shows that it is a java.lang.RuntimeException. I've been through all of the forum posts on this, and have changed my code accordingly, but the problem still persists. Help?
Code:
public class DroidzActivity extends Activity implements OnGesturePerformedListener {
/** Called when the activity is first created. */
private GestureLibrary mLibrary;
private static final String TAG = MainThread.class.getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MainGamePanel(this));
//requesting to turn the title OFF
//requestWindowFeature(Window.FEATURE_NO_TITLE);
//make it full screen
//getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// set the MainGamePanel as the View
Log.d(TAG, "View added");
mLibrary = GestureLibraries.fromRawResource(this, R.raw.gestures);
if (!mLibrary.load()) {
finish();
}
GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
gestures.addOnGesturePerformedListener(this);
}
@Override
protected void onDestroy() {
Log.d(TAG, "Destroying...");
super.onDestroy();
}
@Override
protected void onStop() {
Log.d(TAG, "Stopping...");
super.onStop();
}
public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
ArrayList<Prediction> predictions = mLibrary.recognize(gesture);
if (predictions.size() > 0) {
if (predictions.get(0).score > 1.0) {
String action = predictions.get(0).name;
if ("action_add".equals(action)) {
Toast.makeText(this, "Adding a contact", Toast.LENGTH_SHORT).show();
} else if ("action_delete".equals(action)) {
Toast.makeText(this, "Removing a contact", Toast.LENGTH_SHORT).show();
} else if ("action_refresh".equals(action)) {
Toast.makeText(this, "Reloading contacts", Toast.LENGTH_SHORT).show();
}
}
}
}
LogCat:
02-08 23:16:54.898: D/MainThread(3027): View added
02-08 23:16:54.937: D/AndroidRuntime(3027): Shutting down VM
02-08 23:16:54.937: W/dalvikvm(3027): threadid=1: thread exiting with uncaught exception (group=0x40015578)
02-08 23:16:54.949: E/AndroidRuntime(3027): FATAL EXCEPTION: main
02-08 23:16:54.949: E/AndroidRuntime(3027): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.obviam.droidz/net.obviam.droidz.DroidzActivity}: java.lang.NullPointerException
02-08 23:16:54.949: E/AndroidRuntime(3027): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
02-08 23:16:54.949: E/AndroidRuntime(3027): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
02-08 23:16:54.949: E/AndroidRuntime(3027): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-08 23:16:54.949: E/AndroidRuntime(3027): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
02-08 23:16:54.949: E/AndroidRuntime(3027): at android.os.Handler.dispatchMessage(Handler.java:99)
02-08 23:16:54.949: E/AndroidRuntime(3027): at android.os.Looper.loop(Looper.java:130)
02-08 23:16:54.949: E/AndroidRuntime(3027): at android.app.ActivityThread.main(ActivityThread.java:3687)
02-08 23:16:54.949: E/AndroidRuntime(3027): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 23:16:54.949: E/AndroidRuntime(3027): at java.lang.reflect.Method.invoke(Method.java:507)
02-08 23:16:54.949: E/AndroidRuntime(3027): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
02-08 23:16:54.949: E/AndroidRuntime(3027): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-08 23:16:54.949: E/AndroidRuntime(3027): at dalvik.system.NativeStart.main(Native Method)
02-08 23:16:54.949: E/AndroidRuntime(3027): Caused by: java.lang.NullPointerException
02-08 23:16:54.949: E/AndroidRuntime(3027): at net.obviam.droidz.DroidzActivity.onCreate(DroidzActivity.java:42)
02-08 23:16:54.949: E/AndroidRuntime(3027): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-08 23:16:54.949: E/AndroidRuntime(3027): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
02-08 23:16:54.949: E/AndroidRuntime(3027): ... 11 more
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.obviam.droidz"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:debuggable="true">
<activity
android:name=".DroidzActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在
setContentView()
上,您将视图设置为MainGamePanel()
。现在在这一行GestureOverlayView 手势 = (GestureOverlayView) findViewById(R.id.gestures);
您正在尝试从资源标识符获取视图。但资源 id 只会在编译时从 xml 自动生成。您应该将内容视图设置为 xml。或者,如果 MainGamePanel 是 ViewGroup,则创建一个方法以从
MainGamePanel
获取GestureOverlayView
。On
setContentView()
you set your view toMainGamePanel()
. Now on this lineGestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures);
you are trying to get a view from a resource identifier. But resource id will generate automatically only from xml at compile time.You sould set your content view to xml. Or if the MainGamePanel is a ViewGroup create a method to get the
GestureOverlayView
fromMainGamePanel
.最终通过实现 FrameView,然后使用frameLayout.addView() 解决了问题。可以在本教程此处找到更好的解释。
非常感谢所有提供帮助的人!
Ended up resolving the problem by implementing a FrameView, then using frameLayout.addView(). A better explanation can be found in this tutorial here.
A big thanks to everyone who helped!