2012-07-18 126 views
2

我在按下激活我的單選按鈕所在的xml按鈕後遇到應用程序崩潰問題。java.lang.nullpointerexception錯誤:應用程序崩潰

logcat說我的錯誤是java.lang.nullpointerexception。

我已經包括我的logcat,發生錯誤的Java和清單。

我原來有我的RadioButtons問題,在那裏該程序允許我讓他們既檢查,也不可能取消選中。當我改變編碼以包括onCheckChange聽衆時,當我按RadioButton時,該應用現在崩潰。

07-17 18:09:37.067: W/dalvikvm(329): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
07-17 18:09:37.077: E/AndroidRuntime(329): FATAL EXCEPTION: main 
07-17 18:09:37.077: E/AndroidRuntime(329): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.com.proto1/com.example.com.proto1.Voiceprompt}: java.lang.ClassNotFoundException: com.example.com.proto1.Voiceprompt in loader dalvik.system.PathClassLoader[/data/app/com.example.com.proto1-2.apk] 
07-17 18:09:37.077: E/AndroidRuntime(329): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569) 
07-17 18:09:37.077: E/AndroidRuntime(329): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
07-17 18:09:37.077: E/AndroidRuntime(329): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
07-17 18:09:37.077: E/AndroidRuntime(329): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
07-17 18:09:37.077: E/AndroidRuntime(329): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-17 18:09:37.077: E/AndroidRuntime(329): at android.os.Looper.loop(Looper.java:123) 
07-17 18:09:37.077: E/AndroidRuntime(329): at android.app.ActivityThread.main(ActivityThread.java:3683) 
07-17 18:09:37.077: E/AndroidRuntime(329): at java.lang.reflect.Method.invokeNative(Native Method) 
07-17 18:09:37.077: E/AndroidRuntime(329): at java.lang.reflect.Method.invoke(Method.java:507) 
07-17 18:09:37.077: E/AndroidRuntime(329): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
07-17 18:09:37.077: E/AndroidRuntime(329): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
07-17 18:09:37.077: E/AndroidRuntime(329): at dalvik.system.NativeStart.main(Native Method) 
07-17 18:09:37.077: E/AndroidRuntime(329): Caused by: java.lang.ClassNotFoundException: com.example.com.proto1.Voiceprompt in loader dalvik.system.PathClassLoader[/data/app/com.example.com.proto1-2.apk] 
07-17 18:09:37.077: E/AndroidRuntime(329): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240) 
07-17 18:09:37.077: E/AndroidRuntime(329): at java.lang.ClassLoader.loadClass(ClassLoader.java:551) 
07-17 18:09:37.077: E/AndroidRuntime(329): at java.lang.ClassLoader.loadClass(ClassLoader.java:511) 
07-17 18:09:37.077: E/AndroidRuntime(329): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 
07-17 18:09:37.077: E/AndroidRuntime(329): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561) 
07-17 18:09:37.077: E/AndroidRuntime(329): ... 11 more 


    package com.example.com.proto1; 

import android.app.Activity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.RadioGroup.OnCheckedChangeListener; 

public class VoicePrompts extends Activity implements OnCheckedChangeListener { 

    RadioButton on, off, onc, offc; 

    // Button Sound 
    MediaPlayer buttonSound1 = MediaPlayer.create(VoicePrompts.this, R.raw.vpo); 
    MediaPlayer buttonSound2 = MediaPlayer 
      .create(VoicePrompts.this, R.raw.vpof); 
    MediaPlayer buttonSound3 = MediaPlayer 
      .create(VoicePrompts.this, R.raw.vpoc); 
    MediaPlayer buttonSound4 = MediaPlayer.create(VoicePrompts.this, 
      R.raw.vpofc); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.voiceprompts); 

     // Setting up the button references 
     on = (RadioButton) findViewById(R.id.vbutton1); 
     off = (RadioButton) findViewById(R.id.vbutton2); 
     onc = (RadioButton) findViewById(R.id.vbutton1); 
     offc = (RadioButton) findViewById(R.id.vbutton2); 

     buttonSound1 = MediaPlayer.create(VoicePrompts.this, R.raw.vpo); 
     buttonSound2 = MediaPlayer.create(VoicePrompts.this, R.raw.vpof); 
     buttonSound3 = MediaPlayer.create(VoicePrompts.this, R.raw.vpoc); 
     buttonSound4 = MediaPlayer.create(VoicePrompts.this, R.raw.vpofc); 
    } 

    public void onCheckedChanged(RadioGroup arg0, int checkedId) { 
     // TODO Auto-generated method stub 
     switch (checkedId) { 
     case R.id.vbutton1: 
      on.setOnLongClickListener(new View.OnLongClickListener() { 

       public boolean onLongClick(View v) { 
        // TODO Auto-generated method stub 
        try { 
         buttonSound1.prepare(); 
         buttonSound1.start(); 
         startActivity(new Intent("android.intent.action.VPON")); 

        } catch (Exception e) { 

        } 
        return false; 
       } 
      }); 

      onc.setOnClickListener(new View.OnClickListener() { 

       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        try { 
         buttonSound3.prepare(); 
         buttonSound3.start(); 
         startActivity(new Intent("android.intent.action.VPON")); 
        } catch (Exception e) { 

        } 
        ; 

       } 
      }); 

      break; 
     case R.id.vbutton2: 

      off.setOnLongClickListener(new View.OnLongClickListener() { 

       public boolean onLongClick(View v) { 
        // TODO Auto-generated method stub 
        try { 
         buttonSound2.prepare(); 
         buttonSound2.start(); 
         startActivity(new Intent("android.intent.action.VPOFF")); 
        } catch (Exception e) { 

        } 
        ; 
        return false; 

       } 
      }); 

      offc.setOnClickListener(new View.OnClickListener() { 

       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        try { 
         buttonSound4.prepare(); 
         buttonSound4.start(); 
         startActivity(new Intent("android.intent.action.VPOFF")); 
        } catch (Exception e) { 

        } 
        ; 

       } 
      }); 

      break; 
     } 

    } 

} 




<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.com.proto1" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="15" /> 

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".mainj" 
      android:label="@string/title_activity_mainj" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".menu" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MENU" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".Infoactive" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.INFOSCREEN" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".VoicePrompts" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.VOICEPROMPTS" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".VPon" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.VPON" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".VPoff" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.VPOFF" /> 

       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 





package com.example.com.proto1; 

import android.app.Activity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class menu extends Activity { 

    @Override 
    protected void onCreate(Bundle aboutmenu) { 
     // TODO Auto-generated method stub 
     super.onCreate(aboutmenu); 
     setContentView(R.layout.mainx); 

     // Button Sound 
     final MediaPlayer buttonSound = MediaPlayer.create(menu.this, 
       R.raw.button_click); 

     // Setting up the button references 
     Button info = (Button) findViewById(R.id.aboutbutton); 
     Button voice = (Button) findViewById(R.id.voicebutton); 

     info.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       buttonSound.start(); 
       startActivity(new Intent("android.intent.action.INFOSCREEN")); 

      } 
     }); 

     voice.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       buttonSound.start(); 
       Intent voiceIntent = new Intent("android.intent.action.VOICEPROMPTS"); 
       startActivity(voiceIntent); 
      } 
     }); 

    } 
} 
+1

我沒有看到堆棧跟蹤任何空指針初始化......但無論如何,錯誤似乎是由一個ClassNotFoundException引起的,這意味着類加載器是不能夠找到你的班級。 – Shine 2012-07-18 14:48:40

回答

1

我認爲你有兩個活動名稱爲VoicePromptsVoicePrompt。您在清單文件中聲明VoicePrompts。但是你打電話給VoicePrompt

編輯根據您的新問題

的MediaPlayer buttonSound1,buttonSound2,buttonSound3,buttonSound4;

onCreate

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.voiceprompts); 

    // Setting up the button references 
    on = (RadioButton) findViewById(R.id.vbutton1); 
    off = (RadioButton) findViewById(R.id.vbutton2); 
    onc = (RadioButton) findViewById(R.id.vbutton1); 
    offc = (RadioButton) findViewById(R.id.vbutton2); 
    buttonSound1 = MediaPlayer.create(VoicePrompts.this, 
     R.raw.vpo); 
    buttonSound2 = MediaPlayer.create(VoicePrompts.this, 
     R.raw.vpof); 
    buttonSound3 = MediaPlayer.create(VoicePrompts.this, 
     R.raw.vpoc); 
    buttonSound4 = MediaPlayer.create(VoicePrompts.this, 
     R.raw.vpofc); 
} 
+0

我已經在我的帖子編輯中添加了調用該活動的java,我認爲它看起來是正確的。 – 2012-07-18 15:02:16

+0

我已經編輯我的答案,現在看看它.. – 2012-07-18 15:26:17

+0

和請再讀你的問題..這是非常混亂的..'LogCat'反映活動尚未開始。但是你寫'現在應用程序崩潰時,我按下一個RadioButton.'請看看它...並清理你的問題一點點。 – 2012-07-18 15:29:29

2

給按鈕Refrences後setContentView(R.layout.voiceprompts);

一樣,

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.voiceprompts); 

     // Setting up the button references 

      on = (RadioButton) findViewById(R.id.vbutton1); 
      off = (RadioButton) findViewById(R.id.vbutton2); 
      onc = (RadioButton) findViewById(R.id.vbutton1); 
      offc = (RadioButton) findViewById(R.id.vbutton2); 
    } 
+0

我按照以下步驟操作,現在每當按按鈕打開單選按鈕所在的xml時,我都會得到相同的錯誤。 – 2012-07-18 14:44:16

+1

你可以在明確的文件中添加活動嗎?' – 2012-07-18 14:45:37

+0

你能澄清嗎?你說你添加 – 2012-07-18 15:03:56