我对android编程很陌生。我试图通过创建一个设置墙纸背景的简单应用程序来学习 android。我承认android开发中有很多基本概念我仍然不了解和理解,所以请对我好一点。
我使用 Android SDK 4.1,Eclipse Indigo。
我设置了我的 AVD 规格:
CPU/ABI: ARM (armeabi-v7a)
Target: Android 4.1 (API level 16)
Skin: WVGA800
SD Card: 512
每次运行应用程序时都会出错。
错误:
08-11 14:11:51.733: E/Trace(696): error opening trace file: No such file or directory (2)
08-11 14:11:52.383: D/dalvikvm(696): GC_FOR_ALLOC freed 44K, 3% free 8054K/8259K, paused 44ms, total 45ms
08-11 14:11:52.443: I/dalvikvm-heap(696): Grow heap (frag case) to 14.643MB for 7056016-byte allocation
08-11 14:11:52.534: D/dalvikvm(696): GC_CONCURRENT freed 1K, 2% free 14943K/15175K, paused 25ms+5ms, total 85ms
08-11 14:11:53.093: D/dalvikvm(696): GC_FOR_ALLOC freed (ImageView.java:120)
08-11 14:11:53.724: I/dalvikvm(696): at android.widget.ImageView.(ImageView.java:110)
08-11 14:11:53.724: I/dalvikvm(696): at java.lang.reflect.Constructor.constructNative(Native Method)
08-11 14:11:53.724: I/dalvikvm(696): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
08-11 14:11:53.763: I/dalvikvm(696): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
08-11 14:11:53.763: I/dalvikvm(696): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-11 14:11:53.763: I/dalvikvm(696): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
08-11 14:11:53.763: I/dalvikvm(696): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
08-11 14:11:53.763: I/dalvikvm(696): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
08-11 14:11:53.763: I/dalvikvm(696): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
08-11 14:11:53.763: I/dalvikvm(696): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
08-11 14:11:53.763: I/dalvikvm(696): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-11 14:11:53.763: I/dalvikvm(696): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-11 14:11:53.763: I/dalvikvm(696): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-11 14:11:53.763: I/dalvikvm(696): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
08-11 14:11:53.763: I/dalvikvm(696): at android.app.Activity.setContentView(Activity.java:1867)
08-11 14:11:53.763: I/dalvikvm(696): at com.example.androidwallpaper.MainActivity.onCreate(MainActivity.java:12)
08-11 14:11:53.773: I/dalvikvm(696): at android.app.Activity.performCreate(Activity.java:5008)
08-11 14:11:53.773: I/dalvikvm(696): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-11 14:11:53.773: I/dalvikvm(696): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-11 14:11:53.773: I/dalvikvm(696): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-11 14:11:53.773: I/dalvikvm(696): at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-11 14:11:53.773: I/dalvikvm(696): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-11 14:11:53.773: I/dalvikvm(696): at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 14:11:53.773: I/dalvikvm(696): at android.os.Looper.loop(Looper.java:137)
08-11 14:11:53.773: I/dalvikvm(696): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-11 14:11:53.773: I/dalvikvm(696): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 14:11:53.773: I/dalvikvm(696): at java.lang.reflect.Method.invoke(Method.java:511)
08-11 14:11:53.773: I/dalvikvm(696): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-11 14:11:53.773: I/dalvikvm(696): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-11 14:11:53.773: I/dalvikvm(696): at dalvik.system.NativeStart.main(Native Method)
08-11 14:11:53.804: D/skia(696): --- decoder->decode returned false
08-11 14:11:53.804: D/AndroidRuntime(696): Shutting down VM
08-11 14:11:53.804: W/dalvikvm(696): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-11 14:11:53.823: E/AndroidRuntime(696): FATAL EXCEPTION: main
08-11 14:11:53.823: E/AndroidRuntime(696): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidwallpaper/com.example.androidwallpaper.MainActivity}: android.view.InflateException: Binary XML file line #43: Error inflating class
08-11 14:11:53.823: E/AndroidRuntime(696): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.os.Handler.dispatchMessage(Handler.java:99)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.os.Looper.loop(Looper.java:137)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-11 14:11:53.823: E/AndroidRuntime(696): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 14:11:53.823: E/AndroidRuntime(696): at java.lang.reflect.Method.invoke(Method.java:511)
08-11 14:11:53.823: E/AndroidRuntime(696): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-11 14:11:53.823: E/AndroidRuntime(696): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-11 14:11:53.823: E/AndroidRuntime(696): at dalvik.system.NativeStart.main(Native Method)
08-11 14:11:53.823: E/AndroidRuntime(696): Caused by: android.view.InflateException: Binary XML file line #43: Error inflating class
08-11 14:11:53.823: E/AndroidRuntime(696): at android.view.LayoutInflater.createView(LayoutInflater.java:613)
08-11 14:11:53.823: E/AndroidRuntime(696): at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
08-11 14:11:53.823: E/AndroidRuntime(696): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.app.Activity.setContentView(Activity.java:1867)
08-11 14:11:53.823: E/AndroidRuntime(696): at com.example.androidwallpaper.MainActivity.onCreate(MainActivity.java:12)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.app.Activity.performCreate(Activity.java:5008)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-11 14:11:53.823: E/AndroidRuntime(696): ... 11 more
08-11 14:11:53.823: E/AndroidRuntime(696): Caused by: java.lang.reflect.InvocationTargetException
08-11 14:11:53.823: E/AndroidRuntime(696): at java.lang.reflect.Constructor.constructNative(Native Method)
08-11 14:11:53.823: E/AndroidRuntime(696): at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.view.LayoutInflater.createView(LayoutInflater.java:587)
08-11 14:11:53.823: E/AndroidRuntime(696): ... 26 more
08-11 14:11:53.823: E/AndroidRuntime(696): Caused by: java.lang.OutOfMemoryError
08-11 14:11:53.823: E/AndroidRuntime(696): at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:353)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.content.res.Resources.loadDrawable(Resources.java:1930)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.widget.ImageView.(ImageView.java:120)
08-11 14:11:53.823: E/AndroidRuntime(696): at android.widget.ImageView.(ImageView.java:110)
08-11 14:11:53.823: E/AndroidRuntime(696): ... 29 more
08-11 14:11:59.383: I/Process(696): Sending signal. PID: 696 SIG: 9
我的代码如下:
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/IVDisplay"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/wp1" />
<Button
android:id="@+id/bSetWallPaper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Wallpaper" />
<HorizontalScrollView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="@+id/IVimage1"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/wp1" />
<ImageView
android:id="@+id/IVimage2"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/wp2" />
<ImageView
android:id="@+id/IVimage3"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/wp3" />
<ImageView
android:id="@+id/IVimage4"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/wp4" />
<ImageView
android:id="@+id/IVimage5"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/wp5" />
<ImageView
android:id="@+id/IVimage6"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/wp6" />
<ImageView
android:id="@+id/IVimage7"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/wp7" />
<ImageView
android:id="@+id/IVimage8"
android:layout_width="125dp"
android:layout_height="125dp"
android:padding="15dp"
android:src="@drawable/wp8" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
MainActivity.java
package com.example.androidwallpaper;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void setWallPaper (View view) {
new AlertDialog.Builder(this).setTitle("Codes").setMessage("Put codes here later!").
setNeutralButton("Close", null).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androidwallpaper"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
下面是错误的屏幕截图。
下面是图形布局的屏幕截图。
请帮忙。我真的很需要帮助。