gpt4 book ai didi

java - 尝试创建位图时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 22:16:48 25 4
gpt4 key购买 nike

当我尝试将应用程序放入后台堆栈时,我收到了 NullPointerException。以下是应用程序失败的堆栈跟踪:

06-04 15:03:08.715 E/AndroidRuntime(22756): Process: com.example.jalexander.copyright, PID: 22756
06-04 15:03:08.715 E/AndroidRuntime(22756): java.lang.RuntimeException: Unable to stop activity {com.example.jalexander.copyright/com.example.jalexander.copyright.MainActivity}: java.lang.NullPointerException
06-04 15:03:08.715 E/AndroidRuntime(22756): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3202)
06-04 15:03:08.715 E/AndroidRuntime(22756): at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3253)
06-04 15:03:08.715 E/AndroidRuntime(22756): at android.app.ActivityThread.access$1100(ActivityThread.java:139)
06-04 15:03:08.715 E/AndroidRuntime(22756): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
06-04 15:03:08.715 E/AndroidRuntime(22756): at android.os.Handler.dispatchMessage(Handler.java:102)
06-04 15:03:08.715 E/AndroidRuntime(22756): at android.os.Looper.loop(Looper.java:136)
06-04 15:03:08.715 E/AndroidRuntime(22756): at android.app.ActivityThread.main(ActivityThread.java:5103)
06-04 15:03:08.715 E/AndroidRuntime(22756): at java.lang.reflect.Method.invokeNative(Native Method)
06-04 15:03:08.715 E/AndroidRuntime(22756): at java.lang.reflect.Method.invoke(Method.java:515)
06-04 15:03:08.715 E/AndroidRuntime(22756): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
06-04 15:03:08.715 E/AndroidRuntime(22756): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
06-04 15:03:08.715 E/AndroidRuntime(22756): at dalvik.system.NativeStart.main(Native Method)
06-04 15:03:08.715 E/AndroidRuntime(22756): Caused by: java.lang.NullPointerException
06-04 15:03:08.715 E/AndroidRuntime(22756): at android.graphics.Bitmap.createBitmap(Bitmap.java:664)
06-04 15:03:08.715 E/AndroidRuntime(22756): at android.graphics.Bitmap.createBitmap(Bitmap.java:632)
06-04 15:03:08.715 E/AndroidRuntime(22756): at com.example.jalexander.copyright.MainActivity.onStop(MainActivity.java:102)
06-04 15:03:08.715 E/AndroidRuntime(22756): at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1212)
06-04 15:03:08.715 E/AndroidRuntime(22756): at android.app.Activity.performStop(Activity.java:5420)
06-04 15:03:08.715 E/AndroidRuntime(22756): at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3199)
06-04 15:03:08.715 E/AndroidRuntime(22756): ... 11 more

下面是onStop()方法的源代码。我用 ***s 突出显示了第 102 行:

{
super.onStop();
try{
Display display = getWindowManager().getDefaultDisplay();

int tlx = (168/800)*display.getWidth();
int tly = (136/480)*display.getHeight();
int brx = (631/800)*display.getWidth();
int bry = (343/480)*display.getHeight();

int rotation = display.getRotation();


Bitmap q = BitmapFactory.decodeFile("R.drawable.stockpic");

***Bitmap template = Bitmap.createBitmap(q, 168, 136, (631-168), (343-136));***
View v1 = getWindow().getDecorView();

Bitmap current;
Process p = Runtime.getRuntime().exec("su");
boolean landscape = false;
boolean touch = false;


do{rotation = display.getRotation();
if(rotation == 90 || rotation == 270){landscape = true;}
else{rotation = display.getRotation();}


}while(rotation != 90 && rotation != 270);

while(landscape == true){

v1.setDrawingCacheEnabled(true);
current = Bitmap.createBitmap(v1.getDrawingCache(), 1686, 136, (631-168), (343-136));
v1.setDrawingCacheEnabled(false);
touch = current.sameAs(template);
if(touch == true){
p.getOutputStream().write("input tap 750 324.7".getBytes());
touch= false;
}
else{}
}}catch(IOException e){}}

最佳答案

R.drawable.stockpic 是一个资源名称(无论如何都不应该用引号引起来),BitmapFactory.decodeFile() 需要一个文件名。根据documentation ,如果图像数据无法解码则返回null。

正确的做法是

Bitmap q = BitmapFactory.decodeResource(yourContext.getResources(), R.drawable.stockpic);

关于java - 尝试创建位图时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30823084/

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