简体   繁体   English

android应用程序中的java.lang.RuntimeException

[英]java.lang.RuntimeException with android 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. 检查LogCat显示它是一个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: 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>

On setContentView() you set your view to MainGamePanel() . setContentView() ,将视图设置为MainGamePanel() Now on this line GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures); 现在在此行上GestureOverlayView 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. 但是资源ID将仅在编译时从xml自动生成。

You sould set your content view to xml. 您已将内容视图设置为xml。 Or if the MainGamePanel is a ViewGroup create a method to get the GestureOverlayView from MainGamePanel . 或者,如果MainGamePanel是ViewGroup中创建一个方法来获得GestureOverlayViewMainGamePanel

Ended up resolving the problem by implementing a FrameView, then using frameLayout.addView(). 最终通过实现FrameView,然后使用frameLayout.addView()解决了该问题。 A better explanation can be found in this tutorial here. 可以在本教程中找到更好的解释

A big thanks to everyone who helped! 非常感谢所有帮助的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM