android - 在 Android Eclipse 中旋转图像时出错

标签 android eclipse

这是我的主要代码..

public class MainActivity extends Activity {

private static int RESULT_LOAD_IMAGE = 1;
private Spinner spinner1;
Bitmap b;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     addListenerOnSpinnerItemSelection();

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


}

/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}*/


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    ImageView imageView = (ImageView) findViewById(R.id.imgView);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        b = BitmapFactory.decodeFile(picturePath);

        imageView.setImageBitmap(b);

    }


}

public void addListenerOnSpinnerItemSelection() {
    ImageView imageView = (ImageView) findViewById(R.id.imgView);
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new CustomOnItemSelected(imageView, this , b));
  }

}

这是另一个类..

public class CustomOnItemSelected implements OnItemSelectedListener {

ImageView i;
Context c;
private Bitmap myBitmap;

CustomOnItemSelected(ImageView i,Context c, Bitmap b)
{
    this.i = i;
    this.c = c;
    myBitmap = b;
}

//private static final float ROTATE_FROM = 0.0f;

//private static final float ROTATE_TO = -10.0f * 360.0f;

public void onItemSelected(AdapterView parent, View view, int pos, long id) { Toast.makeText(parent.getContext(), "OnItemSelectedListener : "+ parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();

   if(parent.getItemAtPosition(pos).toString().equals("Rotate"))
  {

       Matrix mat = new Matrix();
        mat.postRotate(90);
        Bitmap bMapRotate = Bitmap.createBitmap(myBitmap, 0, 0, myBitmap.getWidth(), myBitmap.getHeight(), mat, true); //GIVING ERROR ON THIS LINE...
        i.setImageBitmap(bMapRotate);
    }

它给出了空指针异常错误。请帮助我,出了什么问题?

最佳答案

您的 CustomOnItemSelected 对象是在 addListenerOnSpinnerItemSelection() 中创建的,它是在调用 b(您的位图)之前从 onCreate 调用的。因此,当分配给 myBitmap 时 b 为空。

将您的 addListenerOnSpinnerItemSelection() 调用移至 onActivityResult() 以确保您的位图在调用 CustomOnItemSelected 构造函数之前创建。

关于android - 在 Android Eclipse 中旋转图像时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14562535/

相关文章:

eclipse - 如何忽略Eclipse中的LessJS CSS错误?

java - 无法启动 WAR,嵌套异常是 java.lang.NoClassDefFoundError :

python - 有没有办法让 PyUnit 在 PyDev 中完成后保持显示?

android - Titanium - 在设备上安全地存储信息

java - 如何让 SWT 中的 TreeViewer 正确刷新?

android - 我可以调试不是我创建的 .apk 吗

java - 如何以同步方式包装黑盒异步调用?

java - 如何手动安装 Java Eclipse 的 e(fx)clipse 插件?

android - 使用 AppBarLayout.Behavior 将 AppBarLayout 与 NestedScrollView 流畅地结合在一起

android - Flutter中的"Error: Member not found: ' packageRoot ' "