我是Android编程的新手,并尝试将图像按钮添加到布局中,但是当我启动应用程序时,它会向我显示消息"不幸的是应用程序已停止",我尝试了给出的解决方案在这里,但似乎没有一个工作。请查看并告诉我问题所在以及如何纠正。 非常感谢你的时间
这是logcat文件
10-18 08:21:35.667: E/AndroidRuntime(3856): FATAL EXCEPTION: main
10-18 08:21:35.667: E/AndroidRuntime(3856): Process: com.dkdsoft.dkdsoftux, PID: 3856
10-18 08:21:35.667: E/AndroidRuntime(3856): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dkdsoft.dkdsoftux/com.dkdsoft.dkdsoftux.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class android.widget.ImageButton
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.app.ActivityThread.access$800(ActivityThread.java:139)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.os.Handler.dispatchMessage(Handler.java:102)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.os.Looper.loop(Looper.java:136)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.app.ActivityThread.main(ActivityThread.java:5086)
10-18 08:21:35.667: E/AndroidRuntime(3856): at java.lang.reflect.Method.invoke(Native Method)
10-18 08:21:35.667: E/AndroidRuntime(3856): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-18 08:21:35.667: E/AndroidRuntime(3856): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-18 08:21:35.667: E/AndroidRuntime(3856): Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class android.widget.ImageButton
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
10-18 08:21:35.667: E/AndroidRuntime(3856): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
10-18 08:21:35.667: E/AndroidRuntime(3856): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.app.Activity.setContentView(Activity.java:1937)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:217)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:77)
10-18 08:21:35.667: E/AndroidRuntime(3856): at com.dkdsoft.dkdsoftux.MainActivity.onCreate(MainActivity.java:13)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.app.Activity.performCreate(Activity.java:5248)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
10-18 08:21:35.667: E/AndroidRuntime(3856): ... 9 more
10-18 08:21:35.667: E/AndroidRuntime(3856): Caused by: java.lang.reflect.InvocationTargetException
10-18 08:21:35.667: E/AndroidRuntime(3856): at java.lang.reflect.Constructor.newInstance(Native Method)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
10-18 08:21:35.667: E/AndroidRuntime(3856): ... 25 more
10-18 08:21:35.667: E/AndroidRuntime(3856): Caused by: android.content.res.Resources$NotFoundException: File res/drawable-hdpi/button1.xml from drawable resource ID #0x7f020057
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.content.res.Resources.loadDrawable(Resources.java:2099)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.widget.ImageView.<init>(ImageView.java:139)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.widget.ImageButton.<init>(ImageButton.java:87)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.widget.ImageButton.<init>(ImageButton.java:83)
10-18 08:21:35.667: E/AndroidRuntime(3856): ... 27 more
10-18 08:21:35.667: E/AndroidRuntime(3856): Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: invalid drawable tag Selector
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:933)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
10-18 08:21:35.667: E/AndroidRuntime(3856): at android.content.res.Resources.loadDrawable(Resources.java:2095)
10-18 08:21:35.667: E/AndroidRuntime(3856): ... 31 more
这是MainActivity.java文件
package com.dkdsoft.dkdsoftux;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是button1.xml
<?xml version="1.0" encoding="utf-8"?>
<Selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/button1_pressed" />
<item android:state_focused="true"
android:drawable="@drawable/button1_focused" />
<item android:drawable="@drawable/button1_normal" />
</Selector>
这是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: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="com.dkdsoft.dkdsoftux.MainActivity" >
<ImageButton android:id="@+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/button1"
android:paddingTop="5px">
</ImageButton>
</RelativeLayout>
这是清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dkdsoft.dkdsoftux"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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>
答案 0 :(得分:0)
根据异常(Resources$NotFoundException: File res/drawable-hdpi/button1.xml
),它无法找到button1.xml
文件。确保它在你项目的res/drawable
文件夹中。如果它存在,刷新项目,清理它并重新构建。