【发布时间】:2012-01-31 21:18:17
【问题描述】:
在开始之前让我说我过去两天一直在寻找解决错误,确实找到了一些对我不起作用的可用解决方案。所以最后我在这里发布我的代码:
关于代码:
我想编写一个 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 是否全部来自您的清单?如果是这样,你的按钮和东西应该在布局文件中声明,而不是在你的清单中。我想如果您尝试在这样的清单中声明视图,您会遇到各种奇怪的错误。此外,如果您可以在日志中发布更多位于“java.lang.NullPointerException”下方的行,它会告诉您 java 文件中的哪一行导致空指针
-
Manifest 中不应包含任何布局 XML。它需要在自己的 XML 文件中,在 res 文件夹中。
-
我是新手,所以我可能会指出错误。如果我用错误的术语说任何话,请纠正我。我发布的 xml 代码来自 AndroidManifest.xml ,我认为它是一个布局文件。
-
AndroidManifest.xml 不是布局文件。如果您签入 /res 文件夹,您应该会看到 main.xml。那是一个布局文件。将任何新的 XML 布局文件添加到该文件夹。在您的 onCreate 中,您可以看到它指定 Activity 正在使用的布局文件的位置: setContentView(R.layout.main);
-
是的,我在 /res/layout 文件夹中找到了它,并建议我修改 main.xml,现在我可以在屏幕上看到该按钮。谢谢大家:)
标签: java android xml button android-audiomanager