当我尝试向其添加图像按钮时,我的应用程序崩溃了

时间:2013-11-17 10:29:14

标签: android imagebutton

Android新功能,我已按照this tutorial将“图片”按钮添加到我的应用中,但它失败了。

package com.example.rgm;

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

import com.example.rgm.R;

public class MainActivity extends Activity {

    ImageButton playButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        addListenerOnButton();

    }

    public void addListenerOnButton() {

        playButton = (ImageButton) findViewById(R.id.playButton);

        // button action
        playButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
               Toast.makeText(MainActivity.this,
                "ImageButton is clicked!", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

这是我的activity_main.xml内容

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="fill"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ImageButton
        android:id="@+id/playButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="118dp"
        android:background="@style/AppBaseTheme"
        android:contentDescription="@string/play"
        android:src="@drawable/play"
    />

</RelativeLayout>

这是我的资源/文件夹

enter image description here

这是我的LogCat内容

11-17 05:31:10.435: D/AndroidRuntime(1799): Shutting down VM
11-17 05:31:10.435: W/dalvikvm(1799): threadid=1: thread exiting with uncaught exception (group=0xb1a75b90)
11-17 05:31:10.505: E/AndroidRuntime(1799): FATAL EXCEPTION: main
11-17 05:31:10.505: E/AndroidRuntime(1799): Process: com.example.rgm, PID: 1799
11-17 05:31:10.505: E/AndroidRuntime(1799): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.rgm/com.example.rgm.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class android.widget.ImageButton
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.app.ActivityThread.access$700(ActivityThread.java:135)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.os.Looper.loop(Looper.java:137)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.app.ActivityThread.main(ActivityThread.java:4998)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at java.lang.reflect.Method.invokeNative(Native Method)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at java.lang.reflect.Method.invoke(Method.java:515)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at dalvik.system.NativeStart.main(Native Method)
11-17 05:31:10.505: E/AndroidRuntime(1799): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class android.widget.ImageButton
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.view.LayoutInflater.createView(LayoutInflater.java:620)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.app.Activity.setContentView(Activity.java:1928)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at com.example.rgm.MainActivity.onCreate(MainActivity.java:19)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.app.Activity.performCreate(Activity.java:5243)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
11-17 05:31:10.505: E/AndroidRuntime(1799):     ... 11 more
11-17 05:31:10.505: E/AndroidRuntime(1799): Caused by: java.lang.reflect.InvocationTargetException
11-17 05:31:10.505: E/AndroidRuntime(1799):     at java.lang.reflect.Constructor.constructNative(Native Method)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.view.LayoutInflater.createView(LayoutInflater.java:594)
11-17 05:31:10.505: E/AndroidRuntime(1799):     ... 24 more
11-17 05:31:10.505: E/AndroidRuntime(1799): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f060000 a=-1 r=0x7f060000}
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.content.res.Resources.loadDrawable(Resources.java:2068)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.view.View.<init>(View.java:3545)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.widget.ImageView.<init>(ImageView.java:123)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.widget.ImageButton.<init>(ImageButton.java:87)
11-17 05:31:10.505: E/AndroidRuntime(1799):     at android.widget.ImageButton.<init>(ImageButton.java:83)
11-17 05:31:10.505: E/AndroidRuntime(1799):     ... 27 more

1 个答案:

答案 0 :(得分:3)

尝试删除此行

        android:background="@style/AppBaseTheme"