2013-11-20 101 views
0

我一直在嘗試使用庫中的照片設置主要活動的背景。我添加了一個按鈕並設置了它的監聽器。在該監聽器,我所限定的意圖對結果如下:如何使用庫中的照片設置佈局的背景?

Intent intent = new Intent(); 
     intent.setType("image/*"); 
     intent.setAction(Intent.ACTION_GET_CONTENT); 
     startActivityForResult(Intent.createChooser(intent, "Select Picture"), 
       0); 

我用來接收結果,並設置用於主相對佈局的背景的方法是:

@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; 
     } 
    } 
} 

一旦按鈕是按下,畫廊打開,我可以選擇照片,但之後它崩潰。碰撞發生時的logCat是:

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 <1K, 7% free 10785K/11591K, paused 14ms+2ms, total 37ms 
11-20 21:32:53.721: I/webclipboard(16734): clipservice: [email protected] 
11-20 21:32:53.791: I/webclipboard(16734): clipservice: [email protected] 
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: [email protected] 
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 = [email protected] = 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: <html><head><script src="http://media.admob.com/sdk-core-v40.js"></script><script>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});</script></head><body></body></html> 

這段代碼有什麼不對嗎? 謝謝

+0

向我們展示了崩潰的日誌跟蹤。 –

+0

'CalculationActivity.java:827'上有什麼? –

+0

@AnkitPopli這是第827行的代碼:'if(fromUnits.length> from && fromUnits.length> to){ \t \t \t fromUnitText = fromUnits [from]; \t \t \t toUnitText = fromUnits [to]; \t \t}' –

回答

1

您的第一個問題是由於fromUnits返回的null值。第二次當你按下時你的應用程序崩潰,因爲即使結果不正確,你也試圖獲取數據字符串。即在if (resultCode == RESULT_OK)之外。要麼完全刪除該語句,因爲您沒有使用該語句,要麼在if條件內移動該語句。

0

啓動意圖採摘圖像:

Intent galleryIntent = new Intent(Intent.ACTION_PICK, 
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
startActivityForResult(galleryIntent, 002); 

收到您的形象在這裏:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
if (requestCode == 002 && resultCode == Activity.RESULT_OK) { 
     Uri selectedImage = data.getData(); 
     //Get the picked image uri 
     String picturePath = getRealPathFromURI(selectedImage); 
     Bitmap bmp= BitmapFactory.decodeFile(picturePath, null); 
     bg.setBackgroundDrawable(bmp); 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
}