为什么启动活动停止?(图像上传活动)

时间:2018-08-07 07:42:49

标签: android firebase-realtime-database firebase-storage

我正在尝试制作一个项目,但发现一些问题!我使用调试器,但找不到运行时错误的问题!请在下面给出调试器错误-

  

致命异常:main。        流程: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) {

    }
}


}

2 个答案:

答案 0 :(得分:0)

您不能将活动投射到侦听器界面

您需要在View.OnClickListener活动中实现PrescriptionUpload

public class PrescriptionUpload extends AppCompatActivity  implements View.OnClickListener

比这样使用

buttonchoose.setOnClickListener( this);
buttonchoose.setOnClickListener( this);

答案 1 :(得分:0)

您正在将当前活动投射到Listener类。鉴于您的代码,这行不通

buttonchoose.setOnClickListener((View.OnClickListener) this);

此强制转换期望this的类型为View.OnClickListener或让this类(PrescriptionUpload)实现该接口。

您可能想要的是

public class YourActivity implements View.OnClickListener