Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to set the background of a layout with a photo from gallery?
    primarykey
    data
    text
    <p>I have been trying to set the background of the main activity with a photo from the gallery. I have added a button and set its listener. In this listener, I defined the intent for result as follows:</p> <pre><code>Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Picture"), 0); </code></pre> <p>The method I used to receive the result and set the background for the main relative layout is:</p> <pre><code>@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case 0: data.getDataString(); if (resultCode == RESULT_OK) { try { Bitmap bitmap = MediaStore.Images.Media.getBitmap( getContentResolver(), data.getData()); RelativeLayout bg = (RelativeLayout) findViewById(R.id.mainRL); Drawable drawable = new BitmapDrawable(getResources(), bitmap); bg.setBackgroundDrawable(drawable); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } break; } } } </code></pre> <p>Once the button is pressed, the gallery opens and I can choose photos but after that it crashes. The logCat when the the crash happened is:</p> <pre><code>11-20 21:32:53.381: D/dalvikvm(16734): GC_FOR_ALLOC freed 77K, 8% free 9300K/10051K, paused 20ms, total 20ms 11-20 21:32:53.391: I/dalvikvm-heap(16734): Grow heap (frag case) to 11.075MB for 1521016-byte allocation 11-20 21:32:53.431: D/dalvikvm(16734): GC_CONCURRENT freed &lt;1K, 7% free 10785K/11591K, paused 14ms+2ms, total 37ms 11-20 21:32:53.721: I/webclipboard(16734): clipservice: android.sec.clipboard.ClipboardExManager@427823b0 11-20 21:32:53.791: I/webclipboard(16734): clipservice: android.sec.clipboard.ClipboardExManager@427823b0 11-20 21:32:53.891: D/dalvikvm(16734): GC_CONCURRENT freed 270K, 8% free 10993K/11847K, paused 13ms+12ms, total 40ms 11-20 21:32:54.012: I/webclipboard(16734): clipservice: android.sec.clipboard.ClipboardExManager@427823b0 11-20 21:32:54.282: D/dalvikvm(16734): DexOpt: --- BEGIN 'ads550927314.jar' (bootstrap=0) --- 11-20 21:32:54.412: D/dalvikvm(16734): GC_CONCURRENT freed 207K, 7% free 11673K/12487K, paused 16ms+16ms, total 87ms 11-20 21:32:54.422: V/webkit(16734): BrowserFrame constructor: this=Handler (android.webkit.BrowserFrame) {4277cc40} 11-20 21:32:54.452: D/ProgressBar(16734): setProgressDrawable mProgressDrawable = null, d = android.graphics.drawable.LayerDrawable@427997a8needUpdate = false 11-20 21:32:54.462: D/ProgressBar(16734): setProgress = 0 11-20 21:32:54.462: D/ProgressBar(16734): setProgress = 0, fromUser = false 11-20 21:32:54.462: D/ProgressBar(16734): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 17 11-20 21:32:54.472: W/ResourceType(16734): Failure getting entry for 0x010802c9 (t=7 e=713) in package 0 (error -75) 11-20 21:33:14.943: W/dalvikvm(16967): threadid=1: thread exiting with uncaught exception (group=0x41c612a0) 11-20 21:33:14.993: E/AndroidRuntime(16967): FATAL EXCEPTION: main 11-20 21:33:14.993: E/AndroidRuntime(16967): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.robotaaly.unitbulletconverter/com.robotaaly.unitbulletconverter.CalculationActivity}: java.lang.NullPointerException 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2117) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.app.ActivityThread.access$700(ActivityThread.java:134) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1218) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.os.Handler.dispatchMessage(Handler.java:99) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.os.Looper.loop(Looper.java:137) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.app.ActivityThread.main(ActivityThread.java:4867) 11-20 21:33:14.993: E/AndroidRuntime(16967): at java.lang.reflect.Method.invokeNative(Native Method) 11-20 21:33:14.993: E/AndroidRuntime(16967): at java.lang.reflect.Method.invoke(Method.java:511) 11-20 21:33:14.993: E/AndroidRuntime(16967): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007) 11-20 21:33:14.993: E/AndroidRuntime(16967): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774) 11-20 21:33:14.993: E/AndroidRuntime(16967): at dalvik.system.NativeStart.main(Native Method) 11-20 21:33:14.993: E/AndroidRuntime(16967): Caused by: java.lang.NullPointerException 11-20 21:33:14.993: E/AndroidRuntime(16967): at com.robotaaly.unitbulletconverter.CalculationActivity.calculateFnc(CalculationActivity.java:827) 11-20 21:33:14.993: E/AndroidRuntime(16967): at com.robotaaly.unitbulletconverter.CalculationActivity.access$0(CalculationActivity.java:825) 11-20 21:33:14.993: E/AndroidRuntime(16967): at com.robotaaly.unitbulletconverter.CalculationActivity$2.onTextChanged(CalculationActivity.java:404) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.widget.TextView.sendOnTextChanged(TextView.java:7398) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.widget.TextView.setText(TextView.java:3730) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.widget.TextView.setText(TextView.java:3585) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.widget.EditText.setText(EditText.java:95) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.widget.TextView.setText(TextView.java:3560) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.widget.TextView.onRestoreInstanceState(TextView.java:3460) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.view.View.dispatchRestoreInstanceState(View.java:12012) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2651) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2651) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2651) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.view.ViewGroup.dispatchRestoreInstanceState(ViewGroup.java:2651) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.view.View.restoreHierarchyState(View.java:11990) 11-20 21:33:14.993: E/AndroidRuntime(16967): at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1826) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.app.Activity.onRestoreInstanceState(Activity.java:949) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.app.Activity.performRestoreInstanceState(Activity.java:921) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1145) 11-20 21:33:14.993: E/AndroidRuntime(16967): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070) 11-20 21:33:14.993: E/AndroidRuntime(16967): ... 11 more 11-20 21:33:15.013: I/Ads(16967): adRequestUrlHtml: &lt;html&gt;&lt;head&gt;&lt;script src="http://media.admob.com/sdk-core-v40.js"&gt;&lt;/script&gt;&lt;script&gt;AFMA_getSdkConstants();AFMA_buildAdURL({"preqs":0,"session_id":"14165905526806045037","seq_num":"1","slotname":"a1524d822019181","u_w":320,"msid":"com.robotaaly.unitbulletconverter","adtest":"on","js":"afma-sdk-a-v6.4.1","bas_off":0,"net":"null","app_name":"4.android.com.robotaaly.unitbulletconverter","hl":"en","gnt":8,"carrier":"50212","u_audio":1,"kw":[],"u_sd":1.5,"ms":"-JTEbDWqE5u7nUwF2aVwW9cvXVI5UmAeTnAwp_7nyxZY6Az-TVS6TEfG4RGORwQh4OqQNvA4oR5N_L9JJwubWzQazduuBeiGe7bnMOCDQDehxNgy8uuKeiZUmzaKyoyHopVay-XniTmGhjXJ0QQtpQb1Yc4y0rN87j8xGADQKXxX2aFnGkVp-iwhbRBPetLExiQ_U6m-eVeeogJ7Num_zQksZE7XdOds9ChR0idznXGE0sTZqaGy0O1L8oTsjrs7X4tczIDwM_s-nHjzXEQyUQzunKyzxBLsNNXfiPA_3_t-CoV1YDVB9kNltYsag0QpOFfIRhC0i8LfdcV-02mBTw","mv":"16.android","isu":"27B6214A0270F283668371709772C538","format":"320x50_mb","oar":0,"ad_pos":{"height":0,"visible":0,"y":0,"x":0,"width":0},"u_h":533,"pt":1,"bas_on":0,"ptime":0});&lt;/script&gt;&lt;/head&gt;&lt;body&gt;&lt;/body&gt;&lt;/html&gt; </code></pre> <p>What wrong thing is with this code? Thank you</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload