2012-02-09 1 views
0

J'ai des problèmes avec mon application. Peu de temps après la compilation, l'application se ferme aléatoirement. La vérification de LogCat montre qu'il s'agit d'une exception java.lang.RuntimeException. J'ai parcouru tous les messages du forum à ce sujet, et j'ai changé mon code en conséquence, mais le problème persiste. Aidez-moi?java.lang.RuntimeException avec l'application android

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> 

+1

Veuillez télécharger votre code et le fichier manifeste. –

+3

S'il vous plaît poster votre logcat et le code entier. – Ghost

+0

Si vous lisez l'exception dans LogCat, vous trouverez probablement une ou plusieurs lignes "Caused by" suivies d'une autre exception. Trouvez le dernier "Causé par" et publiez l'exception qui le suit. C'est le problème original. Veuillez le signaler ici et nous pouvons vous aider. –

Répondre

0

Terminé résout le problème en implémentant un FrameView, puis en utilisant frameLayout.addView(). Une meilleure explication peut être trouvée dans ce tutoriel here.

Un grand merci à tous ceux qui ont aidé!

0

Sur setContentView() vous définissez votre vue de MainGamePanel(). Maintenant, sur cette ligne GestureOverlayView gestures = (GestureOverlayView) findViewById(R.id.gestures); vous essayez d'obtenir une vue à partir d'un identifiant de ressource. Mais l'identifiant de ressource ne sera généré automatiquement qu'à partir de xml au moment de la compilation.

Vous devez définir l'affichage de votre contenu sur xml. Ou si le MainGamePanel est un ViewGroup, créez une méthode pour obtenir le GestureOverlayView de MainGamePanel.

+0

Merci beaucoup. Je ne pense pas que MainGamePanel soit un groupe de vue, mais il est un peu intégré dans tout ce que j'ai écrit. À quoi ressemblerait une méthode GestureOverlayView de MainGamePanel? – jmarple

+0

écrivez votre propre méthode. renvoie le 'GettureOverlayView' à la fin si vous en avez un. – Shaiful

Questions connexes