我正在尝试使用壁纸选项创建一个简单的画廊应用程序,但每次我在 a: myWallpaperManager.setBitmap(mBitmap) 行中得到一个空指针异常。这是我的代码:
package com.pbsoft.galeria;
import java.io.IOException;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.app.WallpaperManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class MainActivity extends Activity {
Button button;
Button button2;
ImageView image;
MenuItem item;
int checkImg;
Bitmap mBitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkImg=0;
addListenerOnButton();
}
public void addListenerOnButton() {
image = (ImageView) findViewById(R.id.imageView1);
button = (Button) findViewById(R.id.btnChangeImage);
button2= (Button) findViewById(R.id.btnPrevious);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(checkImg<3)
++checkImg;
else
checkImg=0;
setImg();
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(checkImg>0)
--checkImg;
else
checkImg=3;
setImg();
}
});
}
protected void setImg() {
switch(checkImg){
case 0: image.setImageResource(R.drawable.wilku); break;
case 1: image.setImageResource(R.drawable.kotek); break;
case 2: image.setImageResource(R.drawable.piesek); break;
case 3: image.setImageResource(R.drawable.chomik); break;
}
}
@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);
item = (MenuItem) findViewById(R.id.action_settings);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
mBitmap = BitmapFactory.decodeResource(getResources(),image.getId());
switch (item.getItemId()) {
case R.id.action_settings:
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());//<-this I think could be not created
try{
myWallpaperManager.setBitmap(mBitmap); // <- 91 line, here I get an exception
finish();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
对我来说,它看起来像是 myWallpaperManager 对象的问题——它没有被初始化,但为什么呢?我还在清单中添加了一个权限:“uses-permission android:name="android.permission.SET_WALLPAPER" /”但它仍然不起作用。我对android很陌生,这可能是菜鸟问题,但请帮忙-感谢您的每一个回答!
这也是我的日志:
04-12 12:20:31.180: E/AndroidRuntime(3012): FATAL EXCEPTION: main
04-12 12:20:31.180: E/AndroidRuntime(3012): java.lang.NullPointerException
04-12 12:20:31.180: E/AndroidRuntime(3012): at android.app.WallpaperManager.setBitmap(WallpaperManager.java:494)
04-12 12:20:31.180: E/AndroidRuntime(3012): at com.pbsoft.galeria.MainActivity.onOptionsItemSelected(MainActivity.java:91)
04-12 12:20:31.180: E/AndroidRuntime(3012): at android.app.Activity.onMenuItemSelected(Activity.java:2205)
04-12 12:20:31.180: E/AndroidRuntime(3012): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:777)
04-12 12:20:31.180: E/AndroidRuntime(3012): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
04-12 12:20:31.180: E/AndroidRuntime(3012): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
04-12 12:20:31.180: E/AndroidRuntime(3012): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
04-12 12:20:31.180: E/AndroidRuntime(3012): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
04-12 12:20:31.180: E/AndroidRuntime(3012): at android.view.View$PerformClick.run(View.java:9112)
04-12 12:20:31.180: E/AndroidRuntime(3012): at android.os.Handler.handleCallback(Handler.java:587)
04-12 12:20:31.180: E/AndroidRuntime(3012): at android.os.Handler.dispatchMessage(Handler.java:92)
04-12 12:20:31.180: E/AndroidRuntime(3012): at android.os.Looper.loop(Looper.java:130)
04-12 12:20:31.180: E/AndroidRuntime(3012): at android.app.ActivityThread.main(ActivityThread.java:3835)
04-12 12:20:31.180: E/AndroidRuntime(3012): at java.lang.reflect.Method.invokeNative(Native Method)
04-12 12:20:31.180: E/AndroidRuntime(3012): at java.lang.reflect.Method.invoke(Method.java:507)
04-12 12:20:31.180: E/AndroidRuntime(3012): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
04-12 12:20:31.180: E/AndroidRuntime(3012): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
04-12 12:20:31.180: E/AndroidRuntime(3012): at dalvik.system.NativeStart.main(Native Method)