2013-03-19 104 views
0

同時發動我的安卓模擬器的應用程序,它與以下錯誤 崩潰我附上下面的代碼文件.. 請檢查代碼,並建議我要解決無法啓動活動componentInfo在

錯誤日誌

03-19 17:42:07.613: W/Trace(901): Unexpected value from nativeGetEnabledTags: 0 
03-19 17:42:07.623: W/Trace(901): Unexpected value from nativeGetEnabledTags: 0 
03-19 17:42:08.273: D/dalvikvm(901): GC_FOR_ALLOC freed 64K, 7% free 2514K/2696K, paused 44ms, total 47ms 
03-19 17:42:08.294: I/dalvikvm-heap(901): Grow heap (frag case) to 3.569MB for 1050832-byte allocation 
03-19 17:42:08.354: D/dalvikvm(901): GC_FOR_ALLOC freed 1K, 5% free 3539K/3724K, paused 57ms, total 57ms 
03-19 17:42:08.454: D/dalvikvm(901): GC_CONCURRENT freed <1K, 5% free 3576K/3732K, paused 10ms+22ms, total 100ms 
03-19 17:42:08.614: D/AndroidRuntime(901): Shutting down VM 
03-19 17:42:08.614: W/dalvikvm(901): threadid=1: thread exiting with uncaught exception (group=0x40a70930) 
03-19 17:42:08.624: E/AndroidRuntime(901): FATAL EXCEPTION: main 
03-19 17:42:08.624: E/AndroidRuntime(901): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mypiano/com.example.mypiano.Piano}: java.lang.NullPointerException 
03-19 17:42:08.624: E/AndroidRuntime(901): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
03-19 17:42:08.624: E/AndroidRuntime(901): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
03-19 17:42:08.624: E/AndroidRuntime(901): at android.app.ActivityThread.access$600(ActivityThread.java:141) 
03-19 17:42:08.624: E/AndroidRuntime(901): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
03-19 17:42:08.624: E/AndroidRuntime(901): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-19 17:42:08.624: E/AndroidRuntime(901): at android.os.Looper.loop(Looper.java:137) 
03-19 17:42:08.624: E/AndroidRuntime(901): at android.app.ActivityThread.main(ActivityThread.java:5039) 
03-19 17:42:08.624: E/AndroidRuntime(901): at java.lang.reflect.Method.invokeNative(Native Method) 
03-19 17:42:08.624: E/AndroidRuntime(901): at java.lang.reflect.Method.invoke(Method.java:511) 
03-19 17:42:08.624: E/AndroidRuntime(901): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
03-19 17:42:08.624: E/AndroidRuntime(901): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
03-19 17:42:08.624: E/AndroidRuntime(901): at dalvik.system.NativeStart.main(Native Method) 
03-19 17:42:08.624: E/AndroidRuntime(901): Caused by: java.lang.NullPointerException 
03-19 17:42:08.624: E/AndroidRuntime(901): at com.example.mypiano.Piano.onCreate(Piano.java:118) 
03-19 17:42:08.624: E/AndroidRuntime(901): at android.app.Activity.performCreate(Activity.java:5104) 
03-19 17:42:08.624: E/AndroidRuntime(901): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
03-19 17:42:08.624: E/AndroidRuntime(901): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
03-19 17:42:08.624: E/AndroidRuntime(901): ... 11 more 
03-19 17:42:39.023: I/Process(901): Sending signal. PID: 901 SIG: 9 

Piano.java

package com.example.mypiano; 

import android.app.Activity; 
import android.media.AudioManager; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.RadioButton; 

public class Piano extends Activity { 
MediaPlayer curSnd; 
Button sa,ri,ga,ma,pa,da,ni; 
RadioButton msc,ani,hrn,rb; 
OnClickListener radioListener; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_piano); 
     System.out.print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxxx"); 
     sa = (Button)findViewById(R.id.B1); 
     ri = (Button)findViewById(R.id.B2); 
     ga = (Button)findViewById(R.id.B3); 
     ma = (Button)findViewById(R.id.B4); 
     pa = (Button)findViewById(R.id.B5); 
     da = (Button)findViewById(R.id.B6); 


     AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE); 
     audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0); 


     radioListener = new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 
       System.out.println("coming here 1"); 
       // TODO Auto-generated method stub 
       RadioButton rb= (RadioButton)v; 
       curSnd = MediaPlayer.create(Piano.this,R.raw.rb); 
       playSound(curSnd); 
       switch(rb.getId()) 
       { 
       case R.id.music : System.out.print("MUSIC");break; 
       case R.id.animals:System.out.print("ABIMALS");break; 
       case R.id.horns: System.out.print("HORN");break; 
       } 
      } 
     };  
     msc = (RadioButton)findViewById(R.id.music); 
     msc.setOnClickListener(radioListener); 

     ani = (RadioButton)findViewById(R.id.animals); 
     ani.setOnClickListener(radioListener); 

     hrn = (RadioButton)findViewById(R.id.horns); 
     hrn.setOnClickListener(radioListener); 


     sa.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       System.out.print("SA playing"); 
       // TODO Auto-generated method stub 
       curSnd = MediaPlayer.create(Piano.this,R.raw.sa); 
       curSnd.start(); 
      } 
     }); 
     ri.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       curSnd = MediaPlayer.create(Piano.this,R.raw.ri); 
       curSnd.start(); 
      } 
     }); 
     ga.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       curSnd = MediaPlayer.create(Piano.this,R.raw.ga); 
       curSnd.start(); 
      } 
     }); 
     ma.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       curSnd = MediaPlayer.create(Piano.this,R.raw.ma); 
       curSnd.start(); 
      } 
     }); 
     pa.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       curSnd = MediaPlayer.create(Piano.this,R.raw.pa); 
       curSnd.start(); 
      } 
     }); 
     da.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       curSnd = MediaPlayer.create(Piano.this,R.raw.da); 
       curSnd.start(); 
      } 
     }); 
     ni.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       curSnd = MediaPlayer.create(Piano.this,R.raw.ni); 
       curSnd.start(); 
      } 
     }); 

    } 
    private void playSound(MediaPlayer mediaPlayer) { 
     mediaPlayer.start(); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_piano, menu); 
     return true; 
    } 

} 

的Manifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.mypiano" 
    android:versionCode="1" 
    android:versionName="1.0" > 

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

    <application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.mypiano.Piano" 
     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> 

</manifest> 

activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:background="@drawable/piano" 
    tools:context=".Piano" > 

    <LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <LinearLayout 
     android:layout_width="128dp" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <Button 
     android:id="@+id/B1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="1" /> 

     <Button 
     android:id="@+id/B2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="2" /> 

     <Button 
     android:id="@+id/B3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="3" /> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="128dp" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <Button 
     android:id="@+id/B4" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="4" /> 

     <Button 
     android:id="@+id/B5" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="5" /> 

     <Button 
     android:id="@+id/B6" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="6" /> 

    </LinearLayout> 

    </LinearLayout> 

    <RadioGroup 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <RadioButton 
     android:id="@+id/music" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:checked="true" 
     android:gravity="center_vertical|start|fill_vertical" 
     android:text="MUSIC" /> 

    <RadioButton 
    android:id="@+id/animals" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="ANIMALS" /> 

    <RadioButton 
    android:id="@+id/horns" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="HORN" /> 
</RadioGroup> 
    </LinearLayout> 
+3

發佈您的代碼......沒有任何機構可以回答 – Pragnani 2013-03-19 17:56:20

+0

也請告訴我們您的活動118行中的內容 – smk 2013-03-19 17:56:56

回答

1

看起來你忘了初始化ni

ni = (Button) findViewById (R.id.buttonName); 

所以,你得到null這裏

ni.setOnClickListener(new View.OnClickListener() { 

而且,只是一個建議,但你可以通過XML定義onClickButtons

<Button 
... 
android:onClick="functionName"/> 

然後在代碼中使用一個開關盒或類似定義功能節省了大量的代碼邏輯

public void functionName(View v) 
{ 
    switch (v.getId()) 
    case R.id.button1 
     // do something 

語法可能不完全正確,但應該給你的想法。那麼你沒有所有的聽衆,因爲他們都做同樣的事情,除了設置一個resource

+0

@KCRaju是否解決了您的問題? – codeMagic 2013-03-19 20:15:22

+0

tq它被修改了以上建議。 Tq @codeMagic – KCRaju 2013-03-20 04:41:15

+0

@KCRaju不客氣!如果願意,請接受答案 – codeMagic 2013-03-20 12:40:00

相關問題