我是android的新手所以忍受我...
当我尝试在模拟器中运行程序时,我在android studio中收到错误 我想要一个按钮播放按钮声音.....非常简单吧...
这里是logcat
Process: com.myApp.soundboard, PID: 1046
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.myApp.soundboard/com.myApp.soundboard.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2102)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.access$700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
at android.media.MediaPlayer.create(MediaPlayer.java:844)
at com.myApp.soundboard.MainActivity.<init>(MainActivity.java:19)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2093)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
at android.app.ActivityThread.access$700(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4998)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
at dalvik.system.NativeStart.main(Native Method)
这是主要的代码
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
final MediaPlayer mp_imageButtonClick = MediaPlayer.create(this, R.raw.button7 );
public void imageButtonOnClick(View v)
{
Button imageButton = (Button) v;
((Button) v).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
mp_imageButtonClick.start();
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
任何想法都说它由空指针异常引起,对于知道自己在做什么的人来说可能很简单
答案 0 :(得分:1)
移动此
final MediaPlayer mp_imageButtonClick = MediaPlayer.create(this, R.raw.button7 );
onCreate
中的
MediaPlayer mp_imageButtonClick;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp_imageButtonClick = MediaPlayer.create(this, R.raw.button7 );
也改为
public void imageButtonOnClick(View v)
{
mp_imageButtonClick.start();
}
如果你有
android:onClick:"imageButtonClick"
用于xml中的按钮
答案 1 :(得分:0)
final MediaPlayer mp_imageButtonClick = MediaPlayer.create(this, R.raw.button7 );
此行在您创建对象之前使用'this'引用。由于您尚未创建对象,因此'this'关键字不引用任何内容,并且您获得空指针值。如果将此行移动到onCreate()方法中,则应该没问题。
答案 2 :(得分:0)
您在系统完全初始化之前使用此(MainActivity),这会导致崩溃,因为MediaPlayer.create正在尝试将活动用作上下文。
您可以使用onCreate回调确保您的活动已完全创建:
首先声明您的MediaPlayer变量:
private MediaPlayer mp_imageButtonClick;
然后将剩下的调用移到onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mp_imageButtonClick = MediaPlayer.create(this, R.raw.button7 );
}