【发布时间】:2018-08-07 07:42:48
【问题描述】:
我正在尝试做一个项目,但我发现了一些问题!我使用调试器但找不到运行时错误的问题!请在下面给出调试器错误-
致命异常:主要。 进程:com.example.abed.smit,PID:24486 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.abed.smit/com.example.abed.smit.PrescriptionUpload}:java.lang.ClassCastException:com.example.abed.smit.PrescriptionUpload 无法转换为android.view.View$OnClickListener 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2779) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2844) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572) 在 android.os.Handler.dispatchMessage(Handler.java:110) 在 android.os.Looper.loop(Looper.java:203) 在 android.app.ActivityThread.main(ActivityThread.java:6368) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 原因:java.lang.ClassCastException: com.example.abed.smit.PrescriptionUpload 无法转换为 android.view.View$OnClickListener 在 com.example.abed.smit.PrescriptionUpload.onCreate(PrescriptionUpload.java:33) 在 android.app.Activity.performCreate(Activity.java:6666) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2732) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2844) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1572) 在 android.os.Handler.dispatchMessage(Handler.java:110) 在 android.os.Looper.loop(Looper.java:203) 在 android.app.ActivityThread.main(ActivityThread.java:6368) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)
我的活动的代码如下:
package com.example.abed.smit;
import android.content.Intent;
import android.content.IntentSender;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.IOException;
public class PrescriptionUpload extends AppCompatActivity {
private static final int PICK_IMAGE_REQUEST = 234;
private ImageView imageView;
private Button buttonchoose, buttonUpload;
private Uri filepath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_prescription_upload);
imageView = findViewById(R.id.imageView);
buttonchoose = findViewById(R.id.buttonChoose);
buttonUpload = findViewById(R.id.buttonUpload);
buttonchoose.setOnClickListener((View.OnClickListener) this);
buttonchoose.setOnClickListener((View.OnClickListener) this);
}
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "SELECT AN IMAGE"), PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && requestCode == RESULT_OK && data != null
&& data.getData() != null) {
filepath = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filepath);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void onClick(View view) {
if (view == buttonchoose) {
showFileChooser();
} else if (view == buttonUpload) {
}
}
}
【问题讨论】:
-
` buttonchoose.setOnClickListener((View.OnClickListener) this)` 你的
PrescriptionUpload没有实现View.OnClickListener -
您的
PrescriptionUpload不是View.OnClickListener。你不能投射它。 -
tnx 先生!它的工作
标签: android firebase-realtime-database firebase-storage