Я делаю простой Android-приложение для загрузки фотографий с внешних ресурсов, внутренние ресурсы или рисование OpenGL.Целевая задача не в приложении Android в методе Picasso.with(). Load()
Я пытался фото нагрузки с Пикассо библиотеки для Android, но испытывает:
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>
В чем причина, по которой цель не равна нулю и как я могу исправить код?
Я переместил код в метод onCreate(): 'внешнийPhotoView = (ImageView) findViewById (R.id.externalImage); Picasso.with (getApplicationContext()). Load (R.drawable.example_image) .into (внешнийPhotoView); setContentView (R.layout.activity_photo_from_external_resources) ', но до сих пор цель не пустой :( – gooornik07