I'm trying to make a launcher for Android. And I'm right now working on setting wallpaper from Gallery
. When I open Gallery
, it works fine but when I choose image from Gallery
my app crashes, any solutions? I modified code many time but no success.
Here's my code:
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): Flushing caches (mode 0) 05-22 13:13:38.474: D/OpenGLRenderer(6750): Flushing caches (mode 1) 05-22 13:13:38.591: V/KeyEvent(6750): Reset: android.view.KeyEvent$DispatcherState@41f1e658 05-22 13:13:39.021: D/OpenGLRenderer(6750): Flushing caches (mode 0) 05-22 13:13:43.294: D/dalvikvm(6750): GC_FOR_ALLOC freed 454K, 12% free 15749K/17799K, paused 22ms 05-22 13:13:43.310: I/dalvikvm-heap(6750): Grow heap (frag case) to 20.236MB for 4915216-byte allocation 05-22 13:13:43.349: D/dalvikvm(6750): GC_FOR_ALLOC freed 11K, 10% free 20537K/22663K, paused 27ms 05-22 13:13:43.365: W/CursorWrapperInner(6750): Cursor finalized without prior close() 05-22 13:13:43.419: D/dalvikvm(6750): GC_CONCURRENT freed <1K, 10% free 20538K/22663K, paused 2ms+2ms 05-22 13:13:43.544: D/AndroidRuntime(6750): Shutting down VM 05-22 13:13:43.544: W/dalvikvm(6750): threadid=1: thread exiting with uncaught exception (group=0x40e321f8) 05-22 13:13:43.552: E/AndroidRuntime(6750): FATAL EXCEPTION: main 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): at android.app.Activity.dispatchActivityResult(Activity.java:4829) 05-22 13:13:43.552: E/AndroidRuntime(6750): at android.app.ActivityThread.deliverResults(ActivityThread.java:2996) 05-22 13:13:43.552: E/AndroidRuntime(6750): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3043) 05-22 13:13:43.552: E/AndroidRuntime(6750): at android.app.ActivityThread.access$1100(ActivityThread.java:129) 05-22 13:13:43.552: E/AndroidRuntime(6750): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1183) 05-22 13:13:43.552: E/AndroidRuntime(6750): at android.os.Handler.dispatchMessage(Handler.java:99) 05-22 13:13:43.552: E/AndroidRuntime(6750): at android.os.Looper.loop(Looper.java:137) 05-22 13:13:43.552: E/AndroidRuntime(6750): at 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): Sending signal. PID: 6750 SIG: 9