Java 当我尝试在另一个布局中更改ImageView时,setImageBitmap抛出null错误
我有两个布局Java 当我尝试在另一个布局中更改ImageView时,setImageBitmap抛出null错误,java,android,nullpointerexception,screenshot,Java,Android,Nullpointerexception,Screenshot,我有两个布局Activity\u Main和screenshot。我想从Activity\u Main截图中截图,并将其显示在screenshot public void save() { View rootView = rl.getRootView(); //Activity_Main rootView.setDrawingCacheEnabled(true); Bitmap bitmap =Bitmap.createBitmap(rootView.g
Activity\u Main
和screenshot
。我想从Activity\u Main
截图中截图,并将其显示在screenshot
public void save() {
View rootView = rl.getRootView(); //Activity_Main
rootView.setDrawingCacheEnabled(true);
Bitmap bitmap =Bitmap.createBitmap(rootView.getDrawingCache());
rootView.setDrawingCacheEnabled(false);
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root + "/me");
myDir.mkdirs();
final Dialog savePicDialog = new Dialog(MainActivity.this);
savePicDialog.setTitle("edit name");
savePicDialog.setContentView(R.layout.screenshot);
EditText imgNameTxt = (EditText) savePicDialog.findViewById(R.id.txtPicName);
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmm");
String dateString = sdf.format(date);
imgNameTxt.setText(dateString);
String name = imgNameTxt.getText().toString();
file = new File(myDir, name + ".png");
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, ostream);
ostream.flush();
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
String filePath = file.getPath();
Bitmap selectedBitmap = BitmapFactory.decodeFile(filePath);
screenshotViewCtrl.setImageBitmap(selectedBitmap);
Button saveBtn = (Button) savePicDialog.findViewById(R.id.btnSave);
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newGame();
savePicDialog.cancel();
}
});
Button deleteBtn = (Button) savePicDialog.findViewById(R.id.btnDelete);
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
file.delete();
savePicDialog.cancel();
}
});
savePicDialog.show();}
在onCreate
中,此方法:
public void bind (){
screenshotViewCtrl = (ImageView) findViewById(R.id.screenshotView);
rl = (RelativeLayout) findViewById(R.id.rl) ;}
下面是截图和显示在screenshot
public void save() {
View rootView = rl.getRootView(); //Activity_Main
rootView.setDrawingCacheEnabled(true);
Bitmap bitmap =Bitmap.createBitmap(rootView.getDrawingCache());
rootView.setDrawingCacheEnabled(false);
String root = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString();
File myDir = new File(root + "/me");
myDir.mkdirs();
final Dialog savePicDialog = new Dialog(MainActivity.this);
savePicDialog.setTitle("edit name");
savePicDialog.setContentView(R.layout.screenshot);
EditText imgNameTxt = (EditText) savePicDialog.findViewById(R.id.txtPicName);
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd HHmm");
String dateString = sdf.format(date);
imgNameTxt.setText(dateString);
String name = imgNameTxt.getText().toString();
file = new File(myDir, name + ".png");
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, ostream);
ostream.flush();
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
String filePath = file.getPath();
Bitmap selectedBitmap = BitmapFactory.decodeFile(filePath);
screenshotViewCtrl.setImageBitmap(selectedBitmap);
Button saveBtn = (Button) savePicDialog.findViewById(R.id.btnSave);
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
newGame();
savePicDialog.cancel();
}
});
Button deleteBtn = (Button) savePicDialog.findViewById(R.id.btnDelete);
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
file.delete();
savePicDialog.cancel();
}
});
savePicDialog.show();}
屏幕截图的XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/l1">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/screenshotView"
android:layout_gravity="center_horizontal"/>
<EditText
android:layout_width="200dp"
android:layout_height="wrap_content"
android:id="@+id/txtPicName"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Save"
android:id="@+id/btnSave"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="Delete"
android:id="@+id/btnDelete"
android:layout_gravity="center_horizontal" />
</LinearLayout>
舱单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="alzahrani.alaa.myapp" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
问题出现在screenshotViewCtrl.setImageBitmap(selectedBitmap)中代码>它抛出一个空错误!我试着用同样的布局显示截图,效果很好!但当我试图在“截图”布局中显示它时,它给了我这个错误。
还有一件事。当我创建屏幕截图时,它不会显示在工作室中,直到重新打开AVD!这正常吗
谢谢。您的NullPointerException肯定源于screenshotViewCtrl为null
您通过以下方式在代码中对其进行了初始化:
screenshotViewCtrl=(ImageView)findViewById(R.id.screenshotView)
因此,findviewbyd返回null。这意味着它找不到具有该id的视图。您没有发布onCreate()方法,也没有发布Activity_Main.xml,但我认为您正在膨胀两个布局中的一个,但试图找到另一个中定义的视图。你不能这么做,Android也不会警告你:如果找不到你要找的东西,它只会返回null
如果您想从中访问视图,请确保您正在膨胀screenshot.xml而不是Activity_Main.xml。感谢zsolt.kocsi。这是编辑后的代码:
public void save() {
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View scrnShotLayout = inflater.inflate(R.layout.screenshot, null);
screenshotViewCtrl = (ImageView) scrnShotLayout.findViewById(R.id.screenshotView);
...//same old
savePicDialog.setContentView(menuLayout);
...//same
}
似乎未找到R.id.screenshotView
,导致screenshotViewCtrl
为空。发布你得到的异常。你确定在save()
之前调用了bind()
吗?是的,bind()
在OnCreate
中,save()
在OnClickListener
中,我将尝试将screenshot.xml
作为Activity\u Main.xml
的子级进行充气。