0

我正在尝试做一个壁纸应用程序。我已经完成了,但每次我打开应用程序时都会崩溃。

这是我的活动

package com.wallpaper.rahuldravid;

public class MainActivity extends Activity  {
        Integer [] pics= {
            R.drawable.sunset,
            R.drawable.winter

};
ImageView imageView= (ImageView)findViewById(R.id.ImageView01);;





@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Gallery ga = (Gallery)findViewById(R.id.Gallery01);
ga.setAdapter(new ImageAdapter(this));


ga.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View v, final int pos,long id) {
        Toast.makeText(v.getContext(), "You have selected picture " + (pos+1) +"of Rd", 
            Toast.LENGTH_SHORT).show();
        imageView.setImageResource(pics[pos]);
        imageView.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
            WallpaperManager wm = WallpaperManager.getInstance(v.getContext());
            try {
                wm.setResource(pics[pos]);
            } catch (IOException e) {
                e.printStackTrace();
            }
          }
        });
    }
});

}


@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;
}
 public class ImageAdapter extends BaseAdapter {

private Context ctx;
int imageBackground;

public ImageAdapter(Context c) {
        ctx = c;
        TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
        imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
        ta.recycle();
    }


public int getCount() {

    return pics.length;
}


public Object getItem(int arg0) {

    return arg0;
}


public long getItemId(int arg0) {

    return arg0;
}


public View getView(int arg0, View arg1, ViewGroup arg2) {
    ImageView iv = new ImageView(ctx);
    iv.setImageResource(pics[arg0]);
    iv.setScaleType(ImageView.ScaleType.FIT_XY);
    iv.setLayoutParams(new Gallery.LayoutParams(200,120));
    iv.setBackgroundResource(imageBackground);
    return iv;
}

} }

这是我的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wallpaper.rahuldravid"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="17" />

    <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>
    <uses-permission android:name="android.permission.SET_WALLPAPER" />

</manifest>

这是日志猫:

05-22 19:30:21.008: D/AndroidRuntime(30838): Shutting down VM
05-22 19:30:21.008: W/dalvikvm(30838): threadid=1: thread exiting with uncaught exception (group=0x40a9f210)
05-22 19:30:21.038: E/AndroidRuntime(30838): FATAL EXCEPTION: main
05-22 19:30:21.038: E/AndroidRuntime(30838): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.wallpaper.rahuldravid/com.wallpaper.rahuldravid.MainActivity}: java.lang.NullPointerException
05-22 19:30:21.038: E/AndroidRuntime(30838):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1891)
05-22 19:30:21.038: E/AndroidRuntime(30838):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
05-22 19:30:21.038: E/AndroidRuntime(30838):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
05-22 19:30:21.038: E/AndroidRuntime(30838):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
05-22 19:30:21.038: E/AndroidRuntime(30838):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-22 19:30:21.038: E/AndroidRuntime(30838):    at android.os.Looper.loop(Looper.java:137)
05-22 19:30:21.038: E/AndroidRuntime(30838):    at android.app.ActivityThread.main(ActivityThread.java:4441)
05-22 19:30:21.038: E/AndroidRuntime(30838):    at java.lang.reflect.Method.invokeNative(Native Method)
05-22 19:30:21.038: E/AndroidRuntime(30838):    at java.lang.reflect.Method.invoke(Method.java:511)
05-22 19:30:21.038: E/AndroidRuntime(30838):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
05-22 19:30:21.038: E/AndroidRuntime(30838):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
05-22 19:30:21.038: E/AndroidRuntime(30838):    at dalvik.system.NativeStart.main(Native Method)
05-22 19:30:21.038: E/AndroidRuntime(30838): Caused by: java.lang.NullPointerException
05-22 19:30:21.038: E/AndroidRuntime(30838):    at android.app.Activity.findViewById(Activity.java:1794)
05-22 19:30:21.038: E/AndroidRuntime(30838):    at com.wallpaper.rahuldravid.MainActivity.<init>(MainActivity.java:28)
05-22 19:30:21.038: E/AndroidRuntime(30838):    at java.lang.Class.newInstanceImpl(Native Method)
05-22 19:30:21.038: E/AndroidRuntime(30838):    at java.lang.Class.newInstance(Class.java:1319)
05-22 19:30:21.038: E/AndroidRuntime(30838):    at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
05-22 19:30:21.038: E/AndroidRuntime(30838):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1882)
05-22 19:30:21.038: E/AndroidRuntime(30838):    ... 11 more

我已经进行了全面搜索,我发现的解决方案主要是确保活动名称在代码和清单中是正确的。如您所见,它是正确的。你能帮我解决这个问题吗?

4

2 回答 2

0

在这里:

ImageView imageView= (ImageView)findViewById(R.id.ImageView01);;

您正在尝试imageView在为当前活动设置布局之前初始化实例。onCreate所以在Activity 的方法中移动所有 View 初始化setContentView如下:

// declare all Views here...
ImageView imageView;  //<<<
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     // initialize Views here after setting layout for Activity..
    imageView= (ImageView)findViewById(R.id.ImageView01);  //<<<<

    Gallery ga = (Gallery)findViewById(R.id.Gallery01);
ga.setAdapter(new ImageAdapter(this));
于 2013-05-22T14:14:28.253 回答
0

ImageView imageView= (ImageView)findViewById(R.id.ImageView01);在 onCreate 中设置内容视图之前不应该这样做

于 2013-05-22T14:15:10.140 回答