gpt4 book ai didi

java - 为什么我会遇到类强制转换异常?安卓

转载 作者:行者123 更新时间:2023-12-01 13:01:27 24 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,该应用程序应该加载图库并抓取图像并将其显示在 ImageView 中。我让它工作了,但从“Activity ”更改为“fragment ”以使用 ViewPager,从那时起它就一直崩溃。我相信这是因为我正在使用一种用于 fragment Activity 的方法。

public class Fragment_1 extends Fragment {
private static int RESULT_LOAD_IMAGE = 1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment

Button buttonLoadImage = (Button) getView().findViewById(
R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
Log.v("ButtonPress: ", "Pressed");

Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Log.v("ButtonPress: ", "intent=" + i);

startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
return inflater.inflate(R.layout.fragment_1, container, false);

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_IMAGE && resultCode == Activity.RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();

ImageView imageView = (ImageView) getView().findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

}

}

}

我收到的错误是:

05-05 22:17:59.214: E/AndroidRuntime(32610): FATAL EXCEPTION: main
05-05 22:17:59.214: E/AndroidRuntime(32610): Process: com.example.hashtagged, PID: 32610
05-05 22:17:59.214: E/AndroidRuntime(32610): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.hashtagged/com.example.hashtagged.Fragment_1}: java.lang.ClassCastException: com.example.hashtagged.Fragment_1 cannot be cast to android.app.Activity
05-05 22:17:59.214: E/AndroidRuntime(32610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2131)
05-05 22:17:59.214: E/AndroidRuntime(32610): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2265)
05-05 22:17:59.214: E/AndroidRuntime(32610): at android.app.ActivityThread.access$800(ActivityThread.java:145)
05-05 22:17:59.214: E/AndroidRuntime(32610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
05-05 22:17:59.214: E/AndroidRuntime(32610): at android.os.Handler.dispatchMessage(Handler.java:102)
05-05 22:17:59.214: E/AndroidRuntime(32610): at android.os.Looper.loop(Looper.java:136)
05-05 22:17:59.214: E/AndroidRuntime(32610): at android.app.ActivityThread.main(ActivityThread.java:5142)
05-05 22:17:59.214: E/AndroidRuntime(32610): at java.lang.reflect.Method.invokeNative(Native Method)
05-05 22:17:59.214: E/AndroidRuntime(32610): at java.lang.reflect.Method.invoke(Method.java:515)
05-05 22:17:59.214: E/AndroidRuntime(32610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
05-05 22:17:59.214: E/AndroidRuntime(32610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
05-05 22:17:59.214: E/AndroidRuntime(32610): at dalvik.system.NativeStart.main(Native Method)
05-05 22:17:59.214: E/AndroidRuntime(32610): Caused by: java.lang.ClassCastException: com.example.hashtagged.Fragment_1 cannot be cast to android.app.Activity
05-05 22:17:59.214: E/AndroidRuntime(32610): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
05-05 22:17:59.214: E/AndroidRuntime(32610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2122)
05-05 22:17:59.214: E/AndroidRuntime(32610): ... 11 more

最佳答案

首先尝试在 OnCreateView 上膨胀 View ,如下所示:

private Button mButtonLoadImage;

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1, container, false);
mButtonLoadImage = (Button) view.findViewById(R.id.buttonLoadPicture);
return view;
}

并在onViewCreated中设置onClickListener:

@Override public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mButtonLoadImage.setOnClickListener(onClickLoad);
}

//Good practice
private View.OnClickListener onClickLoad = new View.OnClickListener() {
@Override public void onClick(View view) {
//TODO action here
}
};

关于java - 为什么我会遇到类强制转换异常?安卓,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23485425/

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