我是java的新手,我正在创建一个按钮,一旦按下,你就会得到一个声音简单的东西。 我的代码是MainActivity中的代码:
ImageButton one = (ImageButton) new Button(R.id.imageButton); //error here
final MediaPlayer mp = MediaPlayer.create(this, R.raw.pears);
one.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
mp.start();
}
});
我收到错误“在Button中无法应用于int”,但我的变量不是int类型而是Button类型。 NullPointerException.I也是一个致命的错误,它查找了NullPointerException,看起来你在没有创建对象时会收到错误。但我正在创建ImageButton的对象...... 有什么建议?感谢
这是onCreate方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_item); //layout with ImageButton id imageButton
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
// Home
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Find People
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
//getSupportActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
final Context context = getApplicationContext();
final int duration = Toast.LENGTH_SHORT;
ImageButton one;
try {
one = (ImageButton) findViewById(R.id.imageButton);
one.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.pears);
mp.start();
}
});
}
catch (Exception e){
Log.d("wshhsounds", Log.getStackTraceString(new Exception()));
}
}
根据@Ryan的要求,继承我的布局:
<?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="fill_parent"
android:padding="0dp" >
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:src="@drawable/button"
android:scaleType="fitCenter"
android:background="@color/white"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/blue">
<TextView
android:id="@+id/grid_item_label_static"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="Pears"
android:layout_marginTop="3px"
android:layout_gravity="center"
android:textSize="50px"
android:textColor="@color/white"
android:paddingTop="11dp"
android:paddingBottom="12dp"/>
</LinearLayout>
</LinearLayout>
按照@Ryan的要求,继承了logcat:
03-25 13:57:54.530 13615-13615 / com.non.nonabona.sounds E / AndroidRuntime:致命异常:主要 java.lang.RuntimeException:无法启动活动ComponentInfo {com.non.nonabona.sounds / com.non.nonabona.sounds.MainActivity}: 显示java.lang.NullPointerException 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359) 在android.app.ActivityThread.access $ 700(ActivityThread.java:165) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1326) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:5455) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:525) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1187) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) at dalvik.system.NativeStart.main(Native Method) 引起:java.lang.NullPointerException 在com.non.nonabona.sounds.MainActivity.onCreate(MainActivity.java:76) 在android.app.Activity.performCreate(Activity.java:5372) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2267) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359) 在android.app.ActivityThread.access $ 700(ActivityThread.java:165) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1326) 在android.os.Handler.dispatchMessage(Handler.java:99) 在android.os.Looper.loop(Looper.java:137) 在android.app.ActivityThread.main(ActivityThread.java:5455) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:525) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1187) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 在dalvik.system.NativeStart.main(本地方法)
答案 0 :(得分:4)
喜欢
ImageButton one = (ImageButton)findViewById(R.id.imageButton);
答案 1 :(得分:0)
只需使用
ImageButton one = (ImageButton) findViewByID(R.id.imageButton)
在您的onCreate()
方法中检查您的layout
文件是否合适并且标识为imageButton
的按钮