我正在尝试为Android制作启动器。我现在正致力于设置Gallery
的壁纸。当我打开Gallery
时,它运行正常,但当我从Gallery
我的应用崩溃中选择图片时,是否有任何解决方案?我多次修改代码但没有成功。
这是我的代码:
MainActivity:
public class Preferences extends Activity {
String[] pref_list = { "Wallpaper" };
private static final int REQUEST_CODE = 1;
private Bitmap bitmap;
private ImageView imageView;
RelativeLayout wallpaper;
private static int RESULT_LOAD_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.preferences);
RelativeLayout wallpaper = (RelativeLayout) findViewById(R.id.home_layout);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.lv_activity, pref_list);
ListView listView = (ListView) findViewById(R.id.pref_list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if (position == 0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE);
}
}
});
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
// currImageURI is the global variable I�m using to hold the
// content:// URI of the image
Uri currImageURI = data.getData();
File file = new File(getRealPathFromURI(currImageURI));
if (file.exists()) {
Drawable d = Drawable
.createFromPath(file.getAbsolutePath());
wallpaper.setBackground(d);
}
}
}
}
private String getRealPathFromURI(Uri contentURI) {
Cursor cursor = getContentResolver().query(contentURI, null, null,
null, null);
if (cursor == null) {
return contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor
.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
}
}
logcat的:
05-22 13:13:37.232:D / OpenGLRenderer(6750):刷新缓存(模式0) 05-22 13:13:38.474:D / OpenGLRenderer(6750):刷新缓存(模式1) 05-22 13:13:38.591:V / KeyEvent(6750):重置:android.view.KeyEvent$DispatcherState@41f1e658 05-22 13:13:39.021:D / OpenGLRenderer(6750):刷新缓存(模式0) 05-22 13:13:43.294:D / dalvikvm(6750):GC_FOR_ALLOC释放454K,12%免费15749K / 17799K,暂停22ms 05-22 13:13:43.310:I / dalvikvm-heap(6750):将堆(frag case)增长到20.236MB,用于4915216字节分配 05-22 13:13:43.349:D / dalvikvm(6750):GC_FOR_ALLOC释放11K,10%免费20537K / 22663K,暂停27ms 05-22 13:13:43.365:W / CursorWrapperInner(6750):光标在没有事先关闭的情况下完成() 05-22 13:13:43.419:D / dalvikvm(6750):GC_CONCURRENT释放&lt; 1K,10%free 20538K / 22663K,暂停2ms + 2ms 05-22 13:13:43.544:D / AndroidRuntime(6750):关闭VM 05-22 13:13:43.544:W / dalvikvm(6750):threadid = 1:线程退出未捕获异常(组= 0x40e321f8) 05-22 13:13:43.552:E / AndroidRuntime(6750):致命异常:主要 05-22 13:13:43.552:E / AndroidRuntime(6750):java.lang.NoSuchMethodError:android.widget.RelativeLayout.setBackground 05-22 13:13:43.552:E / AndroidRuntime(6750):at com.avoscent.sl.Preferences.onActivityResult(Preferences.java:76) 05-22 13:13:43.552:E / AndroidRuntime(6750):在android.app.Activity.dispatchActivityResult(Activity.java:4829) 05-22 13:13:43.552:E / AndroidRuntime(6750):在android.app.ActivityThread.deliverResults(ActivityThread.java:2996) 05-22 13:13:43.552:E / AndroidRuntime(6750):在android.app.ActivityThread.handleSendResult(ActivityThread.java:3043) 05-22 13:13:43.552:E / AndroidRuntime(6750):在android.app.ActivityThread.access $ 1100(ActivityThread.java:129) 05-22 13:13:43.552:E / AndroidRuntime(6750):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1183) 05-22 13:13:43.552:E / AndroidRuntime(6750):在android.os.Handler.dispatchMessage(Handler.java:99) 05-22 13:13:43.552:E / AndroidRuntime(6750):在android.os.Looper.loop(Looper.java:137) 05-22 13:13:43.552:E / AndroidRuntime(6750):在android.app.ActivityThread.main(ActivityThread.java:4516) 05-22 13:13:43.552:E / AndroidRuntime(6750):at java.lang.reflect.Method.invokeNative(Native Method) 05-22 13:13:43.552:E / AndroidRuntime(6750):at java.lang.reflect.Method.invoke(Method.java:511) 05-22 13:13:43.552:E / AndroidRuntime(6750):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793) 05-22 13:13:43.552:E / AndroidRuntime(6750):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 05-22 13:13:43.552:E / AndroidRuntime(6750):at dalvik.system.NativeStart.main(Native Method) 05-22 13:13:45.130:I / Process(6750):发送信号。 PID:6750 SIG:9
答案 0 :(得分:0)
wallpaper.setBackground(d);
此方法适用于API 16.如果您使用以下API级别16,则会导致NoSuchMethodException