开始之前让我说,我一直在寻找最后两天解决错误,找到了一些可用的解决方案,没有与我一起工作。所以最后我在这里发布我的代码:Android应用程序返回NULL
关于代码:
我想写一个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
直到现在我无法达成坠机原因。一个类似的线程是在这里不过我无法捕获异常在我的情况 findViewById() returns null for custom component in layout XML, not for other components
我一直在使用最新版本的Android SDK 4.0.3和Esclipse IDE for Java的
任何帮助,将不胜感激,
谢谢
在这个XML都是从你r清单?如果是这样,你的按钮和东西应该在布局文件中声明,而不是在你的清单中声明。我想如果你尝试在清单中声明视图,你会得到各种奇怪的错误。此外,如果您可以在日志中发布更多位于“java.lang.NullPointerException”之下的行,它会告诉您java文件中的哪一行导致空指针 – FoamyGuy 2012-01-31 21:23:10
您不应该有任何布局XML在你的清单中。它需要在它自己的XML文件中,在res文件夹中。 – bschultz 2012-01-31 21:28:09
我是新来的,所以我可以指出它错了。请纠正我,如果我说任何错误terminolgy。 我发布的xml代码来自AndroidManifest.xml,我认为它是一个布局文件。 – 2012-01-31 21:31:30