Nullpointerexception - setImageBitmap

时间:2014-09-28 21:03:54

标签: java android android-layout android-activity

我出乎意料地收到了一个我一直在尝试解决但却不成功的nullpoint异常错误。我已经在下面包含了logcat。

09-28 20:12:29.543: E/AndroidRuntime(3588):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-28 20:26:37.291: E/AndroidRuntime(3778): FATAL EXCEPTION: main
09-28 20:26:37.291: E/AndroidRuntime(3778): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dooba.beta/com.dooba.beta.MoodActivity}: java.lang.NullPointerException
09-28 20:26:37.291: E/AndroidRuntime(3778):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-28 20:26:37.291: E/AndroidRuntime(3778):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-28 20:26:37.291: E/AndroidRuntime(3778):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-28 20:26:37.291: E/AndroidRuntime(3778):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-28 20:26:37.291: E/AndroidRuntime(3778):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-28 20:26:37.291: E/AndroidRuntime(3778):     at android.os.Looper.loop(Looper.java:137)
09-28 20:26:37.291: E/AndroidRuntime(3778):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-28 20:26:37.291: E/AndroidRuntime(3778):     at java.lang.reflect.Method.invokeNative(Native Method)
09-28 20:26:37.291: E/AndroidRuntime(3778):     at java.lang.reflect.Method.invoke(Method.java:525)
09-28 20:26:37.291: E/AndroidRuntime(3778):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-28 20:26:37.291: E/AndroidRuntime(3778):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-28 20:26:37.291: E/AndroidRuntime(3778):     at dalvik.system.NativeStart.main(Native Method)
09-28 20:26:37.291: E/AndroidRuntime(3778): Caused by: java.lang.NullPointerException
09-28 20:26:37.291: E/AndroidRuntime(3778):     at com.dooba.beta.MoodActivity.onCreate(MoodActivity.java:60)
09-28 20:26:37.291: E/AndroidRuntime(3778):     at android.app.Activity.performCreate(Activity.java:5133)
09-28 20:26:37.291: E/AndroidRuntime(3778):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-28 20:26:37.291: E/AndroidRuntime(3778):     ... 11 more

以下是活动代码:

public class MoodActivity extends Activity {

private FeedbackDialog feedBack;
private LocationManager mlocManager; 
private LocationListener mlocListener; 

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mood);
    ImageView ishowsshadow = (ImageView) this.findViewById(R.id.ishows);
    ishowsshadow.setImageBitmap(highlightImage(BitmapFactory.decodeResource(getResources(), R.drawable.band5)));



      ParseAnalytics.trackAppOpened(getIntent());

         // inform the Parse Cloud that it is ready for notifications
         PushService.setDefaultPushCallback(this, matchOptionActivity.class, R.drawable.ic_dooba);
            ParseInstallation.getCurrentInstallation().saveInBackground();
            PushService.subscribe(this, "Updates", LoginActivity.class, R.drawable.ic_dooba);

    feedBack = new FeedbackDialog(this, "AF-46D8F2A319EA-0A");
    mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

    mlocListener = new MyLocationListener();



  if(mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
  {
        mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1800000, 0, mlocListener);
  }
  else
  {

      Context context = getApplicationContext();

      AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
      builder1.setMessage("Your network provider is disabled. We need access to your location as it will help us provide you with the best recommendations. " +
            "We determine your location based on availability of cell tower and WiFi access points. Hence, please configure your network.");
      builder1.setCancelable(true);
      builder1.setPositiveButton("View Settings",
              new DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog, int id) {
              dialog.cancel();

              startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));

          }
      });

      AlertDialog alert11 = builder1.create();
      alert11.setCancelable(false); 
      alert11.show();


  }




public Bitmap highlightImage(Bitmap src) {
      // create new bitmap, which will be painted and becomes result image
      Bitmap bmOut = Bitmap.createBitmap(src.getWidth() + 96, src.getHeight() + 96, Bitmap.Config.ARGB_8888);
      // setup canvas for painting
      Canvas canvas = new Canvas(bmOut);
      // setup default color
      canvas.drawColor(0, PorterDuff.Mode.CLEAR);
      // create a blur paint for capturing alpha
      Paint ptBlur = new Paint();
      ptBlur.setMaskFilter(new BlurMaskFilter(15, Blur.NORMAL));
      int[] offsetXY = new int[2];
      // capture alpha into a bitmap
      Bitmap bmAlpha = src.extractAlpha(ptBlur, offsetXY);
      // create a color paint
      Paint ptAlphaColor = new Paint();
      ptAlphaColor.setColor(0xFFFFFFFF);
      // paint color for captured alpha region (bitmap)
      canvas.drawBitmap(bmAlpha, offsetXY[0], offsetXY[1], ptAlphaColor);
      // free memory
      bmAlpha.recycle();

      // paint the image source
      canvas.drawBitmap(src, 0, 0, null);

      // return out final image
      return bmOut;
     }

第60行,我认为错误位于onCreate:

ishowsshadow.setImageBitmap(highlightImage(BitmapFactory.decodeResource(getResources(), R.drawable.band5)));
下面的

是引用imageview的布局部分

 <ImageView
        android:id="@+id/ishows"
        android:layout_width="160dp"
        android:layout_height="55dp"
        android:layout_alignLeft="@+id/icasual"
        android:layout_below="@+id/textView2"
        android:layout_marginTop="60dp"
        android:contentDescription="@string/experience_eating"
        android:src="@drawable/band5"
        />

1 个答案:

答案 0 :(得分:0)

尝试在源解码中添加选项并修改

getResources()

中的

context.getResources()

BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    image = BitmapFactory.decodeResource(context.getResources(), R.id.myimage, options);