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

标签 java android

当我尝试将应用程序放入后台堆栈时,我收到了 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/

相关文章:

java.lang.ClassNotFoundException : com. ibm.disthub2.impl.client.SessionConfig

java - 您可以通过中心而不是左上角来绑定(bind) JComponent 吗?

android - 引用错误 : Can't find variable __fbBatchedBridge

java.lang.ClassCastException : android. widget.LinearLayout 无法转换为 android.widget.Button

java - 在不丢失多行功能的情况下为 EditText 设置输入类型?

java - 将构造函数传递给方法集

java - 需要与 Java Android Studio 中的 TCP/IP 本地托管服务器通信

android - 连接到 connect() 处的 url 时出现 IO 异常

android - 在 Android APK 中混淆 Assets

android - 在 Eclipse 中,如何从使用它的项目源中快速访问 "android library project"的源?