2011-05-25 14 views
8

मैं अपना पहला एंड्रॉइड ऐप (एक गेम) बनाने की कोशिश कर रहा हूं लेकिन मुझे कुछ कठिनाइयों को शुरू करना है।एंड्रॉइड डेवलपमेंट: "थ्रेड अपरिचित अपवाद के साथ बाहर निकलना"

जब मैं अपने कोड को चलाने मैं इस त्रुटि लॉग मिलती है:

05-25 02:41:51.022: WARN/dalvikvm(634): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
05-25 02:41:51.040: ERROR/AndroidRuntime(634): FATAL EXCEPTION: main 
05-25 02:41:51.040: ERROR/AndroidRuntime(634): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stickfigs.nmg/com.stickfigs.nmg.NMG}: java.lang.NullPointerException 
05-25 02:41:51.040: ERROR/AndroidRuntime(634):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
05-25 02:41:51.040: ERROR/AndroidRuntime(634):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
05-25 02:41:51.040: ERROR/AndroidRuntime(634):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
05-25 02:41:51.040: ERROR/AndroidRuntime(634):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
05-25 02:41:51.040: ERROR/AndroidRuntime(634):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-25 02:41:51.040: ERROR/AndroidRuntime(634):  at android.os.Looper.loop(Looper.java:123) 
05-25 02:41:51.040: ERROR/AndroidRuntime(634):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
05-25 02:41:51.040: ERROR/AndroidRuntime(634):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-25 02:41:51.040: ERROR/AndroidRuntime(634):  at java.lang.reflect.Method.invoke(Method.java:521) 
05-25 02:41:51.040: ERROR/AndroidRuntime(634):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
05-25 02:41:51.040: ERROR/AndroidRuntime(634):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
05-25 02:41:51.040: ERROR/AndroidRuntime(634):  at dalvik.system.NativeStart.main(Native Method) 
05-25 02:41:51.040: ERROR/AndroidRuntime(634): Caused by: java.lang.NullPointerException 
05-25 02:41:51.040: ERROR/AndroidRuntime(634):  at com.stickfigs.nmg.NMG.onCreate(NMG.java:32) 
05-25 02:41:51.040: ERROR/AndroidRuntime(634):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
05-25 02:41:51.040: ERROR/AndroidRuntime(634):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
05-25 02:41:51.040: ERROR/AndroidRuntime(634):  ... 11 more 
05-25 02:41:51.062: WARN/ActivityManager(59): Force finishing activity com.stickfigs.nmg/.NMG 

मुझे लगता है कि समस्या यह "धागा न आया हुआ अपवाद के साथ बाहर निकलने" है हिस्सा है, मुझे पता नहीं है क्या अपवाद हो सकता है या यह क्या पैदा ।

NMGView.java:

यहाँ मेरी कोड है पैकेज com.stickfigs.NMG;

import android.content.Context; 
import android.os.Bundle; 
import android.util.AttributeSet; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 

class NMGView extends SurfaceView implements SurfaceHolder.Callback { 

    class NMGThread extends Thread { 
     //State-tracking constants 
     public static final int STATE_LOSE = 1; 
     public static final int STATE_PAUSE = 2; 
     public static final int STATE_READY = 3; 
     public static final int STATE_RUNNING = 4; 
     public static final int STATE_WIN = 5; 

     /** The state of the game. One of READY, RUNNING, PAUSE, LOSE, or WIN */ 
     private int mode; 

     /** Handle to the surface manager object we interact with */ 
     private SurfaceHolder surfaceHolder; 

     public NMGThread(SurfaceHolder surfaceHolderc, Context contextc) { 
      // get handles to some important objects 
      surfaceHolder = surfaceHolderc; 
      context = contextc; 

     } 

     /** 
     * Restores game state from the indicated Bundle. Typically called when 
     * the Activity is being restored after having been previously 
     * destroyed. 
     * 
     * @param savedState Bundle containing the game state 
     */ 
     public synchronized void restoreState(Bundle savedState) { 
      synchronized (surfaceHolder) { 
       setState(STATE_PAUSE); 
       } 
     } 

     /** 
     * Sets the game mode. That is, whether we are running, paused, in the 
     * failure state, in the victory state, etc. 
     * 
     * @param mode one of the STATE_* constants 
     * @param message string to add to screen or null 
     */ 
     public void setState(int modec) { 
      synchronized (surfaceHolder) { 
       mode = modec; 
      } 
     } 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder holder, int format, int width, 
      int height) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void surfaceCreated(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder holder) { 
     // TODO Auto-generated method stub 

    } 

    /** Handle to the application context, used to e.g. fetch Drawables. */ 
    private Context context; 

    /** The thread that actually draws the animation */ 
    private NMGThread thread; 

    public NMGView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     // register our interest in hearing about changes to our surface 
     SurfaceHolder holder = getHolder(); 
     holder.addCallback(this); 

     // create thread only; it's started in surfaceCreated() 
     thread = new NMGThread(holder, context); 

     setFocusable(true); // make sure we get key events 
    } 

    /** 
    * Fetches the animation thread corresponding to this LunarView. 
    * 
    * @return the animation thread 
    */ 
    public NMGThread getThread() { 
     return thread; 
    } 
} 

NMG.java:

package com.stickfigs.nmg; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Window; 

import com.stickfigs.nmg.NMGView.NMGThread; 

public class NMG extends Activity { 
    /** Called when the activity is first created. */ 

    /** A handle to the thread that's actually running the animation. */ 
    private NMGThread nMGThread; 

    /** A handle to the View in which the game is running. */ 
    private NMGView nMGView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     //Turn off the window's title bar 
     // TODO Turn off the status bar 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 

     // tell system to use the layout defined in our XML file 
     setContentView(R.layout.nmg_layout); 

     // get handles to the LunarView from XML, and its LunarThread 
     nMGView = (NMGView) findViewById(R.id.nmg); 
     nMGThread = nMGView.getThread(); 

     if (savedInstanceState == null) { 
      // we were just launched: set up a new game 
      nMGThread.setState(NMGThread.STATE_READY); 
      Log.w(this.getClass().getName(), "SIS is null"); 
     } else { 
      // we are being restored: resume a previous game 
      nMGThread.restoreState(savedInstanceState); 
      Log.w(this.getClass().getName(), "SIS is nonnull"); 
     } 
    } 
} 

अद्यतन:

R.java: पैकेज com.stickfigs.nmg यहाँ मेरी R.java और nmg_layout.xml है ;

public final class R { 
    public static final class attr { 
    } 
    public static final class drawable { 
     public static final int icon=0x7f020000; 
    } 
    public static final class id { 
     public static final int nmg=0x7f050000; 
    } 
    public static final class layout { 
     public static final int nmg_layout=0x7f030000; 
    } 
    public static final class string { 
     public static final int app_name=0x7f040001; 
     public static final int hello=0x7f040000; 
    } 
} 

nmg_layout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <com.stickfigs.nmg.NMGView 
     android:id="@+id/nmg" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"/> 
</FrameLayout> 
+1

अरे क्या आप मुझे बता सकते हैं कि आपने कैसे हल किया यह त्रुटि सटीक रूप से, क्योंकि मुझे एक ही तरह की त्रुटि मिल रही है, और जब मैं लॉगकैट में उल्लिखित रेखा पर टिप्पणी करता हूं, तो यह अलग-अलग पंक्ति को इंगित करता है।इसकी थोड़ी अजीब ??? –

उत्तर

16

आप स्टैक ट्रेस के माध्यम से देखते हैं, तो आप एक "वजह से ..." रेखा (कभी कभी एक से अधिक) देखेंगे। उनमें से अंतिम एक महत्वपूर्ण है। यह कहता है कि एनएमजी.जावा की लाइन 32 पर एक शून्य सूचक अपवाद था। यही कारण है कि लाइन है, और यह पहले लाइन, कर रहे हैं:

nMGView = (NMGView) findViewById(R.id.nmg); 
nMGThread = nMGView.getThread(); 

जाहिर है, आईडी R.id.nmg के साथ कोई व्यू लेआउट R.layout.nmg_layout में है। यही कारण है कि आपकी समस्या पैदा हो रही है।

+0

यह अजीब बात है क्योंकि यह लेआउट फ़ाइल में मौजूद है और आर में मैंने दोनों को अपने मूल प्रश्न में जोड़ा है। –

+0

एचएम। शायद मैं लाइनों गलत गिनती है? यदि नहीं, तो मैं उन दो पंक्तियों के बीच 'assert nMGView! = Null;' पंक्ति जोड़ने का सुझाव देता हूं। देखें कि क्या एनपीई की बजाय असफल असर के कारण आपका ऐप क्रैश हो जाता है। –

+0

कोशिश की और यह अभी भी पहले के कारण के लिए विफल रहता है। –

1

आपकी XML फ़ाइल में, R.id.nmg मौजूद है। तो मुझे लगता है कि समस्या संसाधन से NMGView ऑब्जेक्ट को बढ़ाकर हुई है। आपको अपने NMGView स्रोत कोड की जांच करनी चाहिए, खासकर इसके निर्माता में।

+0

मुझे यकीन नहीं है कि क्या inflating मतलब है। क्या आप समझ सकते हैं कि आपका क्या मतलब है? –

+0

मुझे नहीं लगता कि यह सही है। यदि समस्या nmg_layout को बढ़ाने के दौरान NMGView को तुरंत चालू करने के कारण हुई थी, तो कॉल को 'setContentView'' पर अपवाद उत्पन्न किया जाएगा। अपवाद बाद में आ रहा है। –

0

यह समस्या तब होती है क्योंकि जब आप सतह दृश्य को नष्ट करते हैं तो अधिकांश समय SurfaceView onDraw() विधि चल रही है, तो आपको एक पूर्ण पॉइंटर त्रुटि मिलती है क्योंकि उस क्षण कैनवास मौजूद नहीं होता है। मैं इस समस्या को NullPointerException के साथ सभी ड्राइंग सामान को पकड़ने तय कर दी है:

@Override 
public void onDraw(Canvas canvas) { 

    try { 
     //Drawing Stuff 

    }catch(NullPointerException e){ 
     Log.e("NULL POINTER EXCEPTION","Canvas NULL POINTER"); 
    } 
} 

आप onPause(), OnDestroy() अपने मुख्य गतिविधि में तरीकों लागू करते हैं तो आप निम्न आदेश मिल जाएगा: पहला: ई/onPause: चालू रोकें -> अगले ई/surfaceDestroyed: सतह नष्ट कर दिया -> ई/नल पॉइंटर एक्सेप्शन: कैनवास नल पॉइंटर -> अंत में ई/OnDestroy: पर नष्ट कर

इस है surfaceDestroyed विधि मैं उपयोग कर रहा हूँ:

@Override 
public void surfaceDestroyed(SurfaceHolder arg0) { 
    Log.e("surfaceDestroyed", "SURFACE DESTROYED "); 


    thread.setRunning(false); 
     try { 
      //thread.setRunning(false); 
      thread.join(); 
      } catch (InterruptedException e) { 
      Log.e("Surface Thread Stopped","SURFACE THREAD STOPPED"); 
     } 

} 
संबंधित मुद्दे