gpt4 book ai didi

java - Android RuntimeException 和 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 09:04:43 26 4
gpt4 key购买 nike

我在从图库中抓取图像并将该图像设置为 RelativeLayout 的背景时遇到这些错误。所以这段代码将在 OnClick() 中打开图库,从图库中抓取一张图像,并将其设置为 RelativeLayout 的背景。

登录目录: Gist

设置 Activity :

MainActivity mainActivity;
RelativeLayout backgroundView;

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

mainActivity = new MainActivity();
backgroundView = (RelativeLayout) findViewById(R.id.wallpaperView);

// Show the Up button in the action bar.
setupActionBar();
}

/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}

public void setBackgroundClick(View v)
{
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}


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

if (resultCode == RESULT_OK){
Uri targetUri = data.getData();
//textTargetUri.setText(targetUri.toString());
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
//imageView.setImageBitmap(bitmap);
backgroundView.setBackgroundDrawable(new BitmapDrawable(bitmap));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
//NavUtils.navigateUpFromSameTask(this);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}

谢谢

最佳答案

从您的日志来看,backgroundView 似乎为空。你能检查 findViewById(R.id.wallpaperView) 是否找到一个 View 吗?

(看起来日志属于 imageView 而不是像日志所说的那样评论:

Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference

所以我认为位图检索没问题,但在您的 View 中设置它是个问题。

关于java - Android RuntimeException 和 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25133665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com