【问题标题】:why launching activity getting stop?(Image upload activity)为什么启动活动停止?(图片上传活动)
【发布时间】: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


【解决方案1】:

您不能将活动投射到侦听器接口

您需要在您的PrescriptionUpload Activity 中实现View.OnClickListener

public class PrescriptionUpload extends AppCompatActivity  implements View.OnClickListener

比这样使用

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

【讨论】:

    【解决方案2】:

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

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

    此转换要求 this 为 View.OnClickListener 类型或让 this 类 (PrescriptionUpload) 实现该接口。

    你可能想要的是

    public class YourActivity implements View.OnClickListener

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-06
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-08
      • 1970-01-01
      相关资源
      最近更新 更多