2012-01-31 10 views
0

私は最後の2日間、エラーを解決するために周りを探索してきたと言いましょう、私と一緒に働かなかったいくつかの使用可能な解決策を見つけました。 SO最終的に私はここに私のコードを掲載しています:Androidアプリケーションの返品NULL

をコードについて:

私はMODE_IN_CALLにオーディオマネージャを設定するシナリオを確認するためにJavaアプリケーションを書きたかったです。

コード:

public class AudioBTActivity extends Activity implements OnClickListener{ 
    /** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 



Button Onbutton = (Button)this.findViewById(R.id.setOn); 
    Button offButton = (Button)this.findViewById(R.id.setOff); 

    // listen for the button being hit 
    Onbutton.setOnClickListener((OnClickListener) this) ; 
    offButton.setOnClickListener((OnClickListener) this); 
    } 

     public void onClick(View v) { 
       switch(v.getId()) { 
       case R.id.setOff: 
        sAudioActivity(); 
        break; 
       case R.id.setOn : 
        AudioActivity(); 
       } 
        Log.e("AudioActivity() failed",null); 

       } 

      public void AudioActivity() { 
       // TODO Auto-generated method stub 
       AudioManager audioMngr = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 
       boolean isInCall = ((audioMngr.getMode() == AudioManager.MODE_IN_CALL)); 
       if(! isInCall){ 
        audioMngr.setMode(AudioManager.MODE_IN_CALL); 
       } 
      } 

     public void sAudioActivity() { 
       // TODO Auto-generated method stub 
      AudioManager audioMngr = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 
      audioMngr.setMode(AudioManager.MODE_NORMAL); 

とxml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.AudioBT" 
    android:versionCode="1" 
    android:versionName="1.0" > 
    <Button 
       android:id="@+id/setOn" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="ON" 
       android:onClick="onClick" 
       > 

      </Button> 

<Button 
       android:id ="@+id/setOff" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="OFF" 
       android:onClick="onClick" 
       > 
       </Button> 
<Button 
       android:id="@+id/strBT" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:onClick="onClick" 
       > 
       </Button> 
    <Button 
       android:id ="@+id/stpBT" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:onClick="onClick" 
       > 
       </Button> 


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

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".AudioBTActivity" 
      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> 

エラー: 01- 31 14:36:53.424:E/AndroidRuntime(1309):java.lang.RuntimeException:アクティビティを開始できませんComponentInfo {com.AudioBT/com.AudioBT.AudioBTActivity}:java.lang.NullPointerException

今まで私はクラッシュの理由に達することができません。同様のスレッドが、私は私の場合 findViewById() returns null for custom component in layout XML, not for other components

で例外をキャッチすることができませんが、私は任意の助けをいただければ幸いJavaの

のためのAndroid SDK 4.0.3とEsclipse IDEの最新バージョンを使用しているここにあります

<Button 
       android:id="@+id/setOn" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="ON" 
       android:onClick="onClick" 
       > 

      </Button> 

<Button 
       android:id ="@+id/setOff" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="OFF" 
       android:onClick="onClick" 
       > 
       </Button> 
<Button 
       android:id="@+id/strBT" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:onClick="onClick" 
       > 
       </Button> 
    <Button 
       android:id ="@+id/stpBT" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:onClick="onClick" 
       > 
       </Button> 
+1

はあなたからすべてがXMLです/ main.xml – FoamyGuy

+0

レイアウトXMLあなたのマニフェストでそれは独自のXMLファイル、resフォルダ内にある必要があります。 – bschultz

+0

私はそれが間違っていると指摘できるので、私は新しいです。私が間違った終末論で何かを言うなら、私を修正してください。 私が投稿したXMLコードはAndroidManifest.xmlからのものです。これはレイアウトファイルと見なされます。 –

答えて

1

は解像度にすべてこのコードを入れてありがとうございましたrマニフェスト?そうであれば、あなたのボタンやものは、マニフェストではなく、レイアウトファイルで宣言する必要があります。私はあなたがそのようなマニフェストでビューを宣言しようとすると、あなたはあらゆる種類の奇妙なエラーを受け取ると思います。あなたのログに "java.lang.NullPointerException"と書かれている行の下にある行をさらにポストできれば、Javaファイルのどの行がNULLポインタを引き起こしているのかがわかるでしょう
関連する問題