我尝试过许多解决办法,但我在评论中很少提到。毫无办法。应用程序在调用startActivityForResult()方法时崩溃。
以下是我所有的源代码:
XML
AndroidManifest.xml<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shubhamr69.app"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="25" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name="com.shubhamr69.app.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
/>
</manifest>activity_main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:orientation="vertical"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/Label1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Progress..."
></TextView>
<Button
android:id="@+id/ImagePicker1"
android:layout_below="@id/Label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="SELECT IMAGE"
android:textColor="@android:color/black"
android:textSize="18sp"
></Button>
<ImageView
android:id="@+id/Image1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/ImagePicker1"
android:layout_marginTop="16dp"
></ImageView>
</LinearLayout>strings.xml<resources>
<string name="app_name">MyApp</string>
<!-- This String is provided by default. -->
<string name="title_activity_main">MAIN_ACTIVITY</string>
</resources>styles.xml<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="@android:style/Theme.Light" />
</resources>JAVA
MainActivity.javapackage com.shubhamr69.app;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.shubhamr69.Components.ImagePicker;
import com.shubhamr69.app.R;
public class MainActivity extends Activity {
ImagePicker imagePicker1;
public static ImageView image1;
public static TextView label1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setup();
}
public void setup(){
imagePicker1 = new ImagePicker();
label1 = findViewById(R.id.Label1);
image1 = findViewById(R.id.Image1);
Button iPB1 = (Button)(findViewById(R.id.ImagePicker1));
// initializing the ImagePicker.
imagePicker1.initialize(iPB1);
imagePicker1.setOnImagePicked(new Runnable(){
@Override
public void run() {
MainActivity.image1.setImageURI(imagePicker1.getContentUri());
MainActivity.label1.setText("Runnable Complete.");
}
});
}
}
***ImagePicker.javapackage com.shubhamr69.app.Components;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import com.shubhamr69.app.MainActivity;
public class ImagePicker extends Activity {
private Button imagePicker;
private Uri contentUri = null;
private String imagePath = "";
public boolean gotImage = false;
public Runnable runnable = new Runnable(){
@Override
public void run(){}
};
public ImagePicker(){
}
// Initializing ImagePicker for a Button.
public void initialize(Button button){
imagePicker = button;
imagePicker.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
MainActivity.label1.setText("Button Clicked.");
// I tried using this both in and out of a new Thread(). Nothing works.
new Thread(){
@Override
public void run(){
chooseImage();
}
}.start();
}
});
}
public void chooseImage(){
MainActivity.label1.setText("chooseImage() Called.");
Intent picker = new Intent();
picker.setType("image/*");
picker.setAction(Intent.ACTION_PICK);
MainActivity.label1.setText("Intent Set and about to be executed.");
// Problem starts here. Everything before this works.
// The ImagePicker doesn't even open and the App crashes.
startActivityForResult(picker, 200);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
MainActivity.label1.setText("Got Intent Result");
contentUri = (resultCode == RESULT_OK && requestCode==200)?data.getData():null;
imagePath = (contentUri != null) ? contentUri.getPath() : "";
gotImage = true;
MainActivity.label1.setText("Starting Run Runnable.");
super.runOnUiThread(runnable);
}
public void setOnImagePicked(Runnable runnableObject){
runnable = runnableObject;
}
public Uri getContentUri(){
return contentUri;
}
public String getPath(){
return imagePath;
}
}TextView Label1只是用来检查应用程序的工作程度。
当我第一次在ImagePicker.java中使用MainActivity.java代码时,一切正常。但是,当我想到将ImagePicker的代码保存在一个单独的类中时,我创建了ImagePicker.java文件,然后将所有内容都移到了那里。
如果在我的代码中找不到错误,至少给我一个工作的src类的ImagePicker.java。
发布于 2022-06-18 06:38:31
我认为您获得了ActivityNotFoundException,因为清单中没有提到您的ImagePicker活动。so you have to addImagePicker`‘be清单和您的清单应该是
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shubhamr69.app"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="25" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name="com.shubhamr69.app.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.shubhamr69.app.ImagePicker"/>
</application>
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
/>
</manifest>https://stackoverflow.com/questions/72667029
复制相似问题