我正在尝试FileOutputStream在画布中绘制的Bitmap,但Logcat给了我:
09-17 14:47:40.203: E/AndroidRuntime(2362): FATAL EXCEPTION: main
09-17 14:47:40.203: E/AndroidRuntime(2362): Process: com.example.drawv2, PID: 2362
09-17 14:47:40.203: E/AndroidRuntime(2362): java.lang.NullPointerException
09-17 14:47:40.203: E/AndroidRuntime(2362): at android.graphics.Bitmap.compress(Bitmap.java:1002)
09-17 14:47:40.203: E/AndroidRuntime(2362): at com.example.drawv2.Scribbler$1.onClick(Scribbler.java:65)
09-17 14:47:40.203: E/AndroidRuntime(2362): at android.view.View.performClick(View.java:4780)
09-17 14:47:40.203: E/AndroidRuntime(2362): at android.view.View$PerformClick.run(View.java:19866)
09-17 14:47:40.203: E/AndroidRuntime(2362): at android.os.Handler.handleCallback(Handler.java:739)
09-17 14:47:40.203: E/AndroidRuntime(2362): at android.os.Handler.dispatchMessage(Handler.java:95)
09-17 14:47:40.203: E/AndroidRuntime(2362): at android.os.Looper.loop(Looper.java:135)
09-17 14:47:40.203: E/AndroidRuntime(2362): at android.app.ActivityThread.main(ActivityThread.java:5257)
09-17 14:47:40.203: E/AndroidRuntime(2362): at java.lang.reflect.Method.invoke(Native Method)
09-17 14:47:40.203: E/AndroidRuntime(2362): at java.lang.reflect.Method.invoke(Method.java:372)
09-17 14:47:40.203: E/AndroidRuntime(2362): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
09-17 14:47:40.203: E/AndroidRuntime(2362): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
这是我的Scribbler课程:
public class Scribbler extends Activity {
DrawView drawView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
drawView = (DrawView) inflater.inflate(R.layout.layoutview, null);
drawView = new DrawView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(30, 30);
drawView.setLayoutParams(layoutParams);
drawView.setBackgroundColor(Color.WHITE);
linearLayout.addView(drawView);
drawView.requestFocus();
drawView.setDrawingCacheEnabled(true);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Bitmap b = Scribbler.this.drawView.getDrawingCache();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(getFilesDir());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
b.compress(CompressFormat.PNG, 95, fos);
}
});
}
}
我有点困惑,因为一般来说Logcat说哪个变量是NullPointerException的原因,但在这里我什么都没得到。
有关信息,我在Cannot refer to the non-final local variable button defined in an enclosing scope
上遇到了drawView
错误,因为我在点击button1
时只需要它的值。我认为异常仍然是由于我的drawView = null
,但我不知道如何解决这个问题。
提前谢谢。
答案 0 :(得分:1)
从logcat中可以清楚地看到,您从该行获取的位图 Scribbler.this.drawView.getDrawingCache()为null。因为它在您尝试压缩位图的行上显示空指针异常,但位图已经为空。
答案 1 :(得分:1)
我认为您的drawView
为空。在使用if
之前,您可以在代码中添加drawView
条件,就像这样。
if(drawView!=null){your code}
或者您可以在控制台上打印视图值以查看其中的内容。
答案 2 :(得分:0)