1

我对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>

下面是错误的屏幕截图。

在此处输入图像描述

下面是图形布局的屏幕截图。

在此处输入图像描述

请帮忙。我真的很需要帮助。

4

2 回答 2

1

1.首先请看你给设置壁纸的正确路径。

2.如果您是 Android 或任何编程语言或框架的新手,请将其分解为多个部分,当您拥有所有功能部分后,将它们组合在一起......

您已经使用过警报对话框、菜单等……但是您是否尝试过将墙纸放在首位,在这种情况下,这是您的主要目标。

OOAD在编写程序时,无论是小程序还是大程序,您都应该应用原则......

这是应用墙纸的 2 行代码,试试这个..然后尝试围绕它构建

WallpaperManager wpm = WallpaperManager.getInstance(Your_Activity.this);
wpm.setBitmap(bit_map_name);
于 2012-08-11T09:34:42.030 回答
1

尝试将您的 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 {

    AlertDialog.Builder builder;

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

        builder = new AlertDialog.Builder(this);
        builder.setTitle("Codes");
        builder.setMessage("Put codes here later!");
        builder.show();
    }
    /*
    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;
    }
}

你也应该可以说builder.setNeutralButton()

于 2012-08-11T07:08:49.860 回答