我现在正在处理一个小测试代码。它选择一个图像,根据屏幕密度调整大小以适应屏幕。当它执行此操作时,它会以xxhdpi
显示中的大小制作图像的副本,并将其转换为位图,然后转换为字符串。然后,通过Intent将String传送到下一个屏幕,在该屏幕中,String将变回Bitmap,然后放入ImageButton
。
这很好用,直到我添加了一大块代码来根据密度调整字符串给出的图像大小。奇怪的是我从另一个活动中复制并粘贴了DPI()方法,并且该活动没有任何问题。
package com.example.zachary.imagetesting.Resizing;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import com.example.zachary.imagetesting.R;
public class PictureDecoded extends Activity {
TextView Text;
ImageButton Image;
String string;
Bitmap picture;
public float DPI(){return getResources().getDisplayMetrics().density;}
float dpi = DPI() * 180;
int size = (int) dpi;
public Bitmap Pic(String string){
try{
byte[] b = Base64.decode(string, Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
return bitmap;
} catch (Exception e) {
e.getMessage();
return null;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.resizing);
string = getIntent().getStringExtra("String");
Image = (ImageButton) findViewById(R.id.image);
Text = (TextView) findViewById(R.id.text);
picture = Pic(string);
Image.setImageBitmap(Bitmap.createScaledBitmap(picture,size,size,false));
}
public void next(View view){ }
// I also tried addring DPI() here as well to no success.
//public float DPI(){return getResources().getDisplayMetrics().density;}
}
01-15 19:55:17.104 30570-30570/com.example.zachary.imagetesting D/ViewRootImpl: ViewPostImeInputStage ACTION_DOWN
01-15 19:55:17.194 30570-30570/com.example.zachary.imagetesting E/Bitmap String:: /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAGVAhwDASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
AAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3
ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWm
p6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEA
AwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSEx
BhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElK
U1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3
uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwDuKKKK
/wAVz/UgKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKK
ACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooA
KKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAo
oooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACii
igAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKK
ACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooA
KKKKACiiigAooooAKKKKACiiigA45GemM+3XGeTjOOPx5ODkruPAuv6DpWppYeMtHj1zwhqk0MOt
2qxqur2EO9h/bHhy/UpcadqtlkTeXFKLTU4l+wanBNA0RTtPjP8ABnU/hRqdldW102ueC/EUa3nh
XxKkaKl1bzo1zFYXqoxEWoQ2jQziQKltf2zpe2aoRd2cPBLMaFLH0svxF6NbEwnPBSk17LFqko+2
p056cuIopqc6EleVKUalKdRRrKMe0ipqnLRyTcG9p2spJa6SW/K9WtU3advE6KKK7ywooooAKKKK
ACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooA
KKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKK0NM0jV9buB
Z6LpWo6vdHAFrpdhd39yckgYgtIZpecf3fT1yfcvDv7Lnxx8Rwpcx+Dn0a3fG2XxFqOn6TLg5wWs
J7htTQcZw1lu68E43cOLzLLcAr47H4TCLS31jE0aLe6SjGpUUpN8rsopvye7iVSnBXnOEF3lJR79
3/dfn69fn2ivtPTP2HPiTcgHVfFXhHSxgErA2salKOuRtOnWMeRgYImYHPJGDXWw/sG6gV/0j4oW
cb4HEPhCeZc5OfmfxJAQMYx8pJJOQNvPh1ON+FKTtLOaLa/590MZWXb4qOHqRf3v9TB43CK968dO
0Zvqlpyxafffa9z8/wCivvO8/YQ8RIrHT/iPo1w38K3nh6/s1PJxuaHU74rkY6I2CSPm25PBap+x
b8Y7EObCfwprQXO1bTWZ7OZ+Wxgatp9jApYAcG6wDn5sAtV0eM+Fq+kM6wsdv40a+GXXVvEUqSS0
1bemt72d3HGYWW1eH/b3NHy+0lf+vU+SaK9G8VfCH4oeCXlHiXwPr9hDCCXv47F9Q0kKCRu/tbTD
e6eBxnDXQbGMgE8+cjB5ByPUexx6nv8ArxknmvoMPicNi6aq4XEUcTSdrVKFWnVptO9rTpylHXld
tfn33jKM1eEoyXeLUl16pvt+ers7lfqF8ItCX43/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KTcm
+rbb829Tibbbbbbe7bd2+7bbf5+reoUUUVIgooooAMf4Z5zjnv1/+sT1GQfDfH37Onwm+IMd7LqH
hq20fWLs7/8AhIfDqx6Vqqz5YmeZYonsNQd8gSHUrK6Yj7jo43V7lRXVg8bjMvrKvgcVXwtZW/eU
Ks6cmoybUZcskpwvq4T5oSTalFq97hOdN81OcoPvFtXt3s9V5PTvc/I34r/so+Pfh6l7rHh/d408
L2y+c93YW7R67YQguJG1HRlMrzRQgAveaZLdRiMNPcwWkQc19r/shaNcaR8DtFmuYmhfW9W17W1R
1KsYZL4aZbSEH+Ga20mKeMnJMcgOSDX03gf5/H/Pr05OKZHHHFGsUSpFGoCqqIERRuP3UQAAclsK
PzJ4+kzXjLMs6yWnlWYU6VSrTxVKu8bBeznVp06deHs6tGK9nzuVRTdSlyRaUYOjf94+mrjKlagq
NRJtSUvaLRtRUrJxStf3m7rva29/xq/akvY7749+P3iZWS2m0KyLD/n
01-15 19:55:17.194 30570-30570/com.example.zachary.imagetesting I/Timeline: Timeline: Activity_launch_request id:com.example.zachary.imagetesting time:50420468
01-15 19:55:17.244 30570-30570/com.example.zachary.imagetesting D/AndroidRuntime: Shutting down VM
01-15 19:55:17.244 30570-30570/com.example.zachary.imagetesting E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.zachary.imagetesting, PID: 30570
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.zachary.imagetesting/com.example.zachary.imagetesting.Resizing.PictureDecoded}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2546)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:90)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
at com.example.zachary.imagetesting.Resizing.PictureDecoded.DPI(PictureDecoded.java:20)
at com.example.zachary.imagetesting.Resizing.PictureDecoded.<init>(PictureDecoded.java:21)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1650)
at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2536)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
大块文本是从传递给Activity的Bitmap转换的String。我留下它以防它有用。大块文本之前的行是按钮单击,它启动了由于NullPointerException导致崩溃的活动的意图。
我可以根据要求添加其他活动,但由于它在我尝试根据屏幕密度调整大小之前有效,我觉得不需要。
答案 0 :(得分:5)
Activity
基础Context
在onCreate()
执行完毕之前一直没有设置。{p}只需在调用dpi
后将onCreate()
的初始化移至super.onCreate()
。