java.lang.InstantiationException: class has no zero argument constructor

本文介绍了解决在Fragment中通过广播接收进度更新时遇到的InstantiationException异常的方法。主要分为两种情况:静态广播注册时需要将广播内部类声明为static类型;非静态广播注册则需在Fragment中完成注册与注销。
java.lang.InstantiationException: class has no zero argument constructor

需要在一个方法中的for循环中获取图片处理进度,在Fragment中显示。在该方法的for循环中采用发送广播的方法,在Fragment中接收进度数据进行显示。在Fragment类中声明了广播内部类。但是运行时出现上面问题。

解决方法如下:

1,如果是静态广播注册方式,广播作为内部类来使用:广播内部类声明为static类型。

2,如果是非静态广播注册方式:广播必须在类中注册(调用registerReceiver()方法)、注销(调用unregisterReceiver()方法)。

项目无法运行package com.example.myapplication; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.RadioGroup; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; public abstract class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener { private static final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(TAG, "onCreate: "); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "onStart: "); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume: "); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause: "); } @Override protected void onStop() { super.onStop(); Log.d(TAG, "onStop: "); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy: "); } }<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="16dp"> <!-- 标题文本 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="请选择选项" android:textSize="18sp" android:layout_marginBottom="16dp" /> <!-- 单选组 --> <RadioGroup android:id="@+id/radio_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginBottom="24dp"> <RadioButton android:id="@+id/radio_option_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项 1" /> <RadioButton android:id="@+id/radio_option_2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项 2" /> <RadioButton android:id="@+id/radio_option_3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选项 3" /> </RadioGroup> <!-- 确认按钮 --> <Button android:id="@+id/btn_confirm" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="确认选择" /> </LinearLayout>
最新发布
10-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值