图像点按钮中的问题
我尝试了许多解决方法,其中很少有人在评论中提到。没有什么可用。该应用程序仅在调用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.java
package 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.java
package 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
class。
I have tried out many workarounds, few of which I've mentioned in the comments. Nothing works. The App crashes just on calling the startActivityForResult()
method.
Here is all my source code :
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.java
package 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.java
package 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 is just for checking upto how much the app works.
When I first used the ImagePicker.java
code in MainActivity.java
, everything was working fine. But when I thought to keep the ImagePicker's code in a separate class, I made the ImagePicker.java
file and then shifted everything there.
If you can't find the mistake in my code, atleast give me the src
for a working ImagePicker.java
class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您有
ActivityNotFoundException
,因为您没有在清单中提到您的ImagePicker活动。因此,您必须将
imagePicker`添加到清单中,并且您表现出来应该是I think you got
ActivityNotFoundException
because you are not mentioned your ImagePicker activity in manifest.so you have to add
ImagePicker` into manifest and you manifest should be