霂似江风 2022-04-24 18:13 采纳率: 100%
浏览 59
已结题

跳转list view界面直接闪退,错误InstantiationException

问题遇到的现象和发生背景

想做一个list view页面,但是跳转到list view界面直接闪退,错误提示list view页面无法实例化

问题相关代码,请勿粘贴截图

.xml代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="one.YIQIlist.one_threeActivity"
    android:orientation="vertical">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@color/blue"
        android:padding="10dp">

        <ImageView
            android:id="@+id/info_iv_flush"
            android:layout_width="28dp"
            android:layout_height="33dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="9dp"
            android:src="@mipmap/clear_btn" />

        <ImageView
            android:id="@+id/info_iv_search"
            android:layout_width="28dp"
            android:layout_height="33dp"
            android:layout_centerVertical="true"
            android:layout_marginLeft="20dp"
            android:layout_marginRight="10dp"
            android:layout_toLeftOf="@id/info_iv_flush"
            android:src="@mipmap/search_btn" />

        <EditText
            android:id="@+id/info_et_search"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/info_iv_search"
            android:background="@drawable/info_et_bg"
            android:paddingLeft="20dp"
            android:textColor="@color/white"
            tools:ignore="SpeakableTextPresentCheck" />

    </RelativeLayout>

    <ListView
        android:id="@+id/one_three_lv"
        android:layout_width="match_parent"
        android:layout_height="718dp"
        android:divider="@color/back_line_1"
        android:dividerHeight="1dp"></ListView>
</LinearLayout>

.activity

package one.YIQIlist;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.ml.sdk.R;

import java.util.ArrayList;
import java.util.List;

import one.bean.YIQIBEAN;
import one.bean.YiQiUitls;

public abstract class one_threeActivity extends AppCompatActivity implements View.OnClickListener {
    EditText searchEt;
    ImageView searchIv,flushIv;
    ListView showlv;
    //  ListView内部数据源
    List<YIQIBEAN>mDatas;
    List<YIQIBEAN>allYIQIList;
    private yiqilist_adapter adapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one_three);
        //查找控件
        initView();
        //2.找到listview对应的数据源
        mDatas=new ArrayList<>();
        allYIQIList = YiQiUitls.getallYIQIList();
        mDatas.addAll(allYIQIList);
        //3.创建适配器 BaseAdapter的子类
        adapter = new yiqilist_adapter(this,mDatas);
        showlv.setAdapter(adapter);//4.设置适配器
//        设置单向点击监听功能
        setListener();
    }

    private void setListener() {
        showlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                YIQIBEAN yiqiBean = mDatas.get(position);
                Intent intent = new Intent(one_threeActivity.this, yiqidescActivity.class);
                intent.putExtra("mingcheng", yiqiBean);
                startActivity(intent);
            }
        });
    }

    private void initView() {
        showlv = findViewById(R.id.one_three_lv);
        searchEt = findViewById(R.id.info_et_search);
        searchIv = findViewById(R.id.info_iv_search);
        flushIv = findViewById(R.id.info_iv_flush);
        searchIv.setOnClickListener(this);
        flushIv.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.info_iv_flush:  //刷新点击
                searchEt.setText("");
                mDatas.clear();
                mDatas.addAll(allYIQIList);
                adapter.notifyDataSetChanged();
                break;
            case R.id.info_iv_search:  //搜索点击
//                1.获取输入内容,判断不为空
                String msg = searchEt.getText().toString().trim();  //获取输入信息
                if (TextUtils.isEmpty(msg)) {
                    Toast.makeText(this,"输入内容不能为空!",Toast.LENGTH_SHORT).show();
                    return;
                }
//                判断所有食物列表的标题是否包含输入内容,如果包含,就添加到小集合中
                List<YIQIBEAN>list = new ArrayList<>();
                for (int i = 0; i < allYIQIList.size(); i++) {
                    String mingcheng = allYIQIList.get(i).getMingcheng();
                    if (mingcheng.contains(msg)) {
                        list.add(allYIQIList.get(i));
                    }
                }
                mDatas.clear();   // 清空ListView的适配器数据源内容
                mDatas.addAll(list);  // 添加新的数据到数据源中
                adapter.notifyDataSetChanged(); // 提示适配器更新
                break;
        }
    }
}

适配器代码

package one.YIQIlist;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ml.sdk.R;

import java.util.List;

import one.bean.YIQIBEAN;

public class yiqilist_adapter extends BaseAdapter {
    Context context;
    List<YIQIBEAN>mDatas;

    public yiqilist_adapter(Context context, List<YIQIBEAN> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }
//决定了listview列表展示的行数
    @Override
    public int getCount() {
        return mDatas.size();
    }
//  返回指定位置对应的数据
    @Override
    public Object getItem(int position) {
        return mDatas.get(position);
    }
//返回指定位置ID
    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.one_three_list_lv,null);//将布局转换为view对象的方法
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        //加载控件显示内容
        //获取集合指定位置的数据
        YIQIBEAN YIQIbean = mDatas.get(position);
        holder.bianmaTv.setText(YIQIbean.getBianma());
        holder.mingchengTv.setText("仪器名称:"+YIQIbean.getMingcheng());
        holder.iv.setImageResource(YIQIbean.getYiqitupian());
        return convertView;
    }

    class ViewHolder {
        ImageView iv;
        TextView bianmaTv, mingchengTv;
        public ViewHolder(View view) {
            iv = view.findViewById(R.id.three_list_iv);
            bianmaTv = view.findViewById(R.id.three_list_tv_bianma);
            mingchengTv = view.findViewById(R.id.three_list_tv_mingcheng);
        }
    }
}


运行结果及报错内容
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.hw.modelart_demo, PID: 1631
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.hw.modelart_demo/one.YIQIlist.one_threeActivity}: java.lang.InstantiationException: java.lang.Class<one.YIQIlist.one_threeActivity> cannot be instantiated
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.InstantiationException: java.lang.Class<one.YIQIlist.one_threeActivity> cannot be instantiated
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
        at android.support.v4.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
I/Process: Sending signal. PID: 1631 SIG: 9


  • 写回答

2条回答 默认 最新

  • weixin_42120514 2022-04-25 09:19
    关注

    你很奇怪啊,包名用大写,类名用小写;
    包名是不能用大写的,所以报错了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 5月3日
  • 已采纳回答 4月25日
  • 创建了问题 4月24日

悬赏问题

  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题