gpt4 book ai didi

java - Android 应用程序中方法 Picasso.with().load() 中的目标不为 null

转载 作者:太空宇宙 更新时间:2023-11-04 12:00:54 25 4
gpt4 key购买 nike

我正在制作简单的Android应用程序来从外部资源加载照片、内部资源或openGL绘制。

我尝试使用 Android 版 picasso 库加载照片,但遇到:

java.lang.IllegalArgumentException: Target must not be null.
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:618)
at com.squareup.picasso.RequestCreator.into(RequestCreator.java:601)
at com.gornik.choosephoto.MainActivity$1.onClick(MainActivity.java:62)
at android.view.View.performClick(View.java:5697)
at android.widget.TextView.performClick(TextView.java:10813)
at android.view.View$PerformClick.run(View.java:22526)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

下面是我的 MainActivity.java 和 Activity_photo_from_external_resources.xml 代码:

    private RadioGroup radioGroup;
private RadioButton radioButton;
private Button submitBtn;
private ImageView externalPhotoView;

private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onClickListener();
}

public void onClickListener(){
radioGroup = (RadioGroup)findViewById(R.id.allOptions);
submitBtn = (Button)findViewById(R.id.button);

submitBtn.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
if(radioGroup.getCheckedRadioButtonId() == -1){
Toast.makeText(MainActivity.this, "Select an option, please", Toast.LENGTH_SHORT).show();
}
else {
int selectedOption = radioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) findViewById(selectedOption);
Toast.makeText(MainActivity.this,
radioButton.getText().toString(),
Toast.LENGTH_SHORT).show();
String verifyOption = radioButton.getText().toString();
switch (verifyOption) {
case "Photo from external resources":
Intent intent = new Intent(MainActivity.this, PhotoFromExternalResourcesActivity.class);
startActivity(intent);
externalPhotoView = (ImageView)findViewById(R.id.externalImage);
Picasso.with(getApplicationContext()).load(R.drawable.example_image).into(externalPhotoView);
break;

case "Photo from Your resources":
verifyStoragePermissions(MainActivity.this);
Intent intent2 = new Intent(MainActivity.this, PhotoFromYourResourceActivity.class);
startActivity(intent2);
break;

case "OpenGL draw":
Intent intent3 = new Intent(MainActivity.this, OpenGLDrawActivity.class);
startActivity(intent3);
break;
}
}
}
}
);
}

public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
}


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.gornik.choosephoto.PhotoFromExternalResourcesActivity">

<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/externalImage"
android:src="@drawable/example_image"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_above="@+id/button2" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="@+id/button2"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />

</RelativeLayout>

目标不为空的原因是什么以及如何更正代码?

最佳答案

您只能访问在当前 Activity 中膨胀的 View 。

您正在尝试访问 MainActivity 内的 externalImage,但 activity_main.xml 中不存在 externalImage,因此 externalPhotoView 引用将始终为 null。

由于 externalImage View 位于 activity_photo_from_external_resources.xml 中,该文件可能在您的 PhotoFromExternalResourcesActivity 类中膨胀,因此您应该将与 Picasso 相关的代码移至您的 PhotoFromExternalResourcesActivity 类。

关于java - Android 应用程序中方法 Picasso.with().load() 中的目标不为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40921334/

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