java - 我无法使用毕加索库显示图片
问题描述
包括来自@Droidman 的 Picasso 库。图书馆真的很不错。但是,如果我宣布有问题,或者我没有在屏幕上正确显示图片。我理解正确,我可以在 .into() 方法中使用带有接收到的 ID 的对象吗?也许我还没有把图书馆连接到最后。
package com.example.my_ugf;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity {
//final public String URL_poster = "https://udmfil.ru/";
public ImageView imageview_poster;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageview_poster.findViewById(R.id.imageview_poster);
Picasso.get().load("/upload/iblock/95a/95aa56e9694ebd46e2855e7167966cfd.jpg")
.resize(50, 50)
.centerCrop()
.into(imageview_poster);
}
}
这就是日志产生的。
2021-11-17 09:46:20.345 29475-29475/com.example.my_ugf E/libc: Access denied finding property "ro.vendor.df.effect.conflict"
2021-11-17 09:46:20.363 29475-29552/com.example.my_ugf E/Perf: Fail to get file list com.example.my_ugf
2021-11-17 09:46:20.367 29475-29552/com.example.my_ugf E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array
2021-11-17 09:46:20.451 29475-29475/com.example.my_ugf D/View: com.example.my_ugf initForcedUseForceDark: 1
2021-11-17 09:46:20.614 29475-29475/com.example.my_ugf D/AndroidRuntime: Shutting down VM
2021-11-17 09:46:20.616 29475-29475/com.example.my_ugf E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.my_ugf, PID: 29475
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.my_ugf/com.example.my_ugf.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(Unknown Source:621)
at android.app.ActivityThread.handleLaunchActivity(Unknown Source:47)
at android.app.servertransaction.LaunchActivityItem.execute(Unknown Source:63)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(Unknown Source:77)
at android.app.servertransaction.TransactionExecutor.execute(Unknown Source:73)
at android.app.ActivityThread$H.handleMessage(Unknown Source:43)
at android.os.Handler.dispatchMessage(Unknown Source:19)
at android.os.Looper.loop(Unknown Source:242)
at android.app.ActivityThread.main(Unknown Source:98)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(Unknown Source:11)
at com.android.internal.os.ZygoteInit.main(Unknown Source:275)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference
at com.example.my_ugf.MainActivity.onCreate(MainActivity.java:18)
at android.app.Activity.performCreate(Unknown Source:19)
at android.app.Activity.performCreate(Unknown Source:1)
at android.app.Instrumentation.callActivityOnCreate(Unknown Source:3)
at android.app.ActivityThread.performLaunchActivity(Unknown Source:376)
at android.app.ActivityThread.handleLaunchActivity(Unknown Source:47)
at android.app.servertransaction.LaunchActivityItem.execute(Unknown Source:63)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(Unknown Source:77)
at android.app.servertransaction.TransactionExecutor.execute(Unknown Source:73)
at android.app.ActivityThread$H.handleMessage(Unknown Source:43)
at android.os.Handler.dispatchMessage(Unknown Source:19)
at android.os.Looper.loop(Unknown Source:242)
at android.app.ActivityThread.main(Unknown Source:98)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(Unknown Source:11)
at com.android.internal.os.ZygoteInit.main(Unknown Source:275)
2021-11-17 09:46:20.624 29475-29475/com.example.my_ugf E/MQSEventManagerDelegate: failed to get MQSService.
2021-11-17 09:46:20.625 29475-29475/? D/OOMEventManagerFK: checkEventAndDumpForJE: 0
2021-11-17 09:46:20.625 29475-29475/? E/MQSEventManagerDelegate: failed to get MQSService.
2021-11-17 09:46:20.653 29475-29475/? I/Process: Sending signal. PID: 29475 SIG: 9
2021-11-17 09:46:19.865 29475-29475/? I/.example.my_ug: Late-enabling -Xcheck:jni
2021-11-17 09:46:19.894 29475-29475/? E/.example.my_ug: Unknown bits set in runtime_flags: 0x8000
2021-11-17 09:46:19.865 29475-29475/? I/.example.my_ug: Late-enabling -Xcheck:jni
2021-11-17 09:46:19.894 29475-29475/? E/.example.my_ug: Unknown bits set in runtime_flags: 0x8000
2021-11-17 09:46:19.865 29475-29475/? I/.example.my_ug: Late-enabling -Xcheck:jni
2021-11-17 09:46:19.894 29475-29475/? E/.example.my_ug: Unknown bits set in runtime_flags: 0x8000
解决方案
例外是
Caused by: java.lang.NullPointerException: println needs a message
这是通过Log.i
调用发生的 - 您null
作为 message 参数传入,这是不允许的。由于您vol
在调用该gtUrlImage
方法之前进行了空值检查,我猜它发生在这里:
// arg array is full of nulls
String[] arg = new String[10];
...
// index is 20 (-1 + 21) if this sequence wasn't found
index = poster.indexOf("m-img buy") + 21;
...
// array entry is only set if index is not 20, i.e. if that sequence was found
if (index!=20) arg[i]= poster.substring(index, index + 55);
// if the sequence wasn't found, arg[i] is still null
Log.i("UGF",arg[i]);
所以基本上,如果你读到一个不包含的行,"m-img buy"
那么该Log
行就会崩溃。您必须弄清楚为什么第二次会得到这些行,并弄清楚如何处理它们 - 或者只是避免记录它们(或用于String.valueOf(arg[i])
安全地将 null 转换为字符串)
推荐阅读
- visual-studio-code - 如何在 VS 代码扩展中访问自动检测到的链接
- python - 使用 random.choice() 随机选择一个动作(定义)
- java - 检查字符串并处理无效日期
- java - 为什么编译器认为 `byte...` 和 `char...` 是模棱两可的
- python - 抓取递归结构时从表格的单元格中获取文本值
- sql-server - 将 SQL 数据库导出到 Excel 缺少映射数据类型,如 int、smallint 等
- java - 将字符串拆分为单词和标点符号,但不要在内部标点符号上拆分
- macros - 宏数值比较
- javascript - 更改只读输入值
- javascript - 如何强制组件在同步请求之间重新渲染?(反应原生)