gpt4 book ai didi

java - Android SDK 空指针异常

转载 作者:行者123 更新时间:2023-11-30 04:36:08 25 4
gpt4 key购买 nike

我想创建一个音板,所以当你点击按钮时它会播放声音。

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.akali);

Button b = (Button) findViewById(R.id.akaliJokeButton);
b.setOnClickListener(new OnClickListener() {


public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(Akali.this, R.raw.akalijoke);
mp.start();
}
});

}

这是我目前拥有的代码。但是我得到一个无指针异常错误

07-25 14:36:47.919: DEBUG/AndroidRuntime(1079): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
07-25 14:36:47.919: DEBUG/AndroidRuntime(1079): CheckJNI is ON
07-25 14:36:48.529: DEBUG/AndroidRuntime(1079): Calling main entry com.android.commands.pm.Pm
07-25 14:36:48.560: DEBUG/AndroidRuntime(1079): Shutting down VM
07-25 14:36:48.569: DEBUG/dalvikvm(1079): GC_CONCURRENT freed 101K, 71% free 297K/1024K, external 0K/0K, paused 1ms+1ms
07-25 14:36:48.579: DEBUG/dalvikvm(1079): Debugger has detached; object registry had 1 entries
07-25 14:36:48.599: INFO/dalvikvm(1079): JNI: AttachCurrentThread (from ???.???)
07-25 14:36:48.599: INFO/AndroidRuntime(1079): NOTE: attach of thread 'Binder Thread #3' failed
07-25 14:36:48.989: DEBUG/AndroidRuntime(1089): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
07-25 14:36:48.989: DEBUG/AndroidRuntime(1089): CheckJNI is ON
07-25 14:36:49.579: DEBUG/AndroidRuntime(1089): Calling main entry com.android.commands.am.Am
07-25 14:36:49.609: INFO/ActivityManager(61): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.reg.lolsoundboard/.Main } from pid 1089
07-25 14:36:49.829: DEBUG/AndroidRuntime(1089): Shutting down VM
07-25 14:36:49.849: DEBUG/dalvikvm(1089): GC_CONCURRENT freed 102K, 69% free 320K/1024K, external 0K/0K, paused 1ms+1ms
07-25 14:36:49.849: DEBUG/jdwp(1089): Got wake-up signal, bailing out of select
07-25 14:36:49.849: DEBUG/dalvikvm(1089): Debugger has detached; object registry had 1 entries
07-25 14:36:49.879: INFO/ActivityManager(61): Start proc com.reg.lolsoundboard for activity com.reg.lolsoundboard/.Main: pid=1099 uid=10036 gids={}
07-25 14:36:49.931: INFO/AndroidRuntime(1089): NOTE: attach of thread 'Binder Thread #3' failed
07-25 14:36:50.839: DEBUG/dalvikvm(1099): GC_EXTERNAL_ALLOC freed 49K, 53% free 2557K/5379K, external 2002K/2137K, paused 54ms
07-25 14:36:51.250: INFO/ActivityManager(61): Displayed com.reg.lolsoundboard/.Main: +1s426ms (total +8m29s588ms)
07-25 14:36:52.179: INFO/ActivityManager(61): Starting: Intent { cmp=com.reg.lolsoundboard/.Akali } from pid 1099
07-25 14:36:52.299: DEBUG/AndroidRuntime(1099): Shutting down VM
07-25 14:36:52.299: WARN/dalvikvm(1099): threadid=1: thread exiting with uncaught exception (group=0x40015560)
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): FATAL EXCEPTION: main
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.reg.lolsoundboard/com.reg.lolsoundboard.Akali}: java.lang.NullPointerException
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): at android.os.Handler.dispatchMessage(Handler.java:99)
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): at android.os.Looper.loop(Looper.java:123)
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): at java.lang.reflect.Method.invokeNative(Native Method)
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): at java.lang.reflect.Method.invoke(Method.java:507)
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): at dalvik.system.NativeStart.main(Native Method)
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): Caused by: java.lang.NullPointerException
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): at com.reg.lolsoundboard.Akali.onCreate(Akali.java:19)
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
07-25 14:36:52.309: ERROR/AndroidRuntime(1099): ... 11 more
07-25 14:36:52.319: WARN/ActivityManager(61): Force finishing activity com.reg.lolsoundboard/.Akali
07-25 14:36:52.329: WARN/ActivityManager(61): Force finishing activity com.reg.lolsoundboard/.Main
07-25 14:36:52.839: WARN/ActivityManager(61): Activity pause timeout for HistoryRecord{406fa098 com.reg.lolsoundboard/.Akali}
07-25 14:36:54.199: INFO/Process(1099): Sending signal. PID: 1099 SIG: 9
07-25 14:36:54.209: INFO/ActivityManager(61): Process com.reg.lolsoundboard (pid 1099) has died.
07-25 14:36:54.209: INFO/WindowManager(61): WIN DEATH: Window{40761880 com.reg.lolsoundboard/com.reg.lolsoundboard.Main paused=true}
07-25 14:36:54.299: WARN/InputManagerService(61): Got RemoteException sending setActive(false) notification to pid 1099 uid 10036

XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/akalibg" android:weightSum="1">
<RelativeLayout android:layout_width="match_parent" android:id="@+id/relativeLayout1" android:layout_height="wrap_content" android:layout_weight="0.33">

<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:id="@+id/akaliLaugh1" android:text="Laugh 1"></Button>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentRight="true" android:text="Laugh 2" android:id="@+id/akaliLaugh2"></Button>
<Button android:text="Laugh 3" android:id="@+id/akaliLaugh3" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignTop="@+id/akaliLaugh4" android:layout_alignLeft="@+id/akaliJokeButton"></Button>
<Button android:id="@+id/akaliLaugh4" android:layout_height="wrap_content" android:keepScreenOn="false" android:layout_width="wrap_content" android:layout_centerVertical="true" android:layout_alignLeft="@+id/akaliLaugh1" android:text="Laugh 4"></Button>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignBottom="@+id/akaliLaugh4" android:layout_alignParentRight="true" android:id="@+id/akaliTaunt" android:layout_alignLeft="@+id/akaliLaugh2" android:text="Taunt 1"></Button>
<Button android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignRight="@+id/akaliLaugh3" android:text="Joke 1"></Button>

</RelativeLayout>
<RelativeLayout android:layout_width="match_parent" android:id="@+id/relativeLayout2" android:layout_height="wrap_content">
</RelativeLayout>


</LinearLayout>

进入实际音板的初始主屏幕

package com.reg.lolsoundboard;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;


public class Main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


ImageButton b = (ImageButton) findViewById(R.id.akaliButton);
b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
startActivity(new Intent(Main.this, Akali.class));

}
});
}
}

最佳答案

您似乎没有 ID 为“akaliJokeButton”的按钮

关于java - Android SDK 空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6817731/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com