繁体   English   中英

Android应用程序返回NULL

[英]Android Application return NULL

在开始之前,我先说说我已经搜寻了最近两天以解决Error,但确实找到了一些对我无效的可用解决方案。 最后,我在这里发布代码:

关于代码:

我想编写一个Java应用程序来检查将音频管理器设置为MODE_IN_CALL的情况。

码:

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

直到现在,我仍无法找到崩溃的原因。 这里有一个类似的线程,但是在我的情况下,我无法捕获异常, 对于布局XML中的自定义组件,findViewById()返回null,而不是其他组件

我一直在使用最新版本的Android SDK 4.0.3和Esclipse IDE for Java

任何帮助,将不胜感激 ,

谢谢

将所有这些代码放在res / main.xml中

   <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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM