gpt4 book ai didi

java - 使用自定义适配器创建列表时出现“无空构造函数”logcat 错误

转载 作者:行者123 更新时间:2023-11-29 20:51:42 25 4
gpt4 key购买 nike

我正在尝试使用自定义适配器填充列表,但是当我尝试运行该应用程序时它崩溃了。检查我的 logcat 我得到这个错误:

03-11 15:33:59.695: E/AndroidRuntime(1200): FATAL EXCEPTION: main
03-11 15:33:59.695: E/AndroidRuntime(1200): Process: com.example.task2_final, PID: 1200
03-11 15:33:59.695: E/AndroidRuntime(1200): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.task2_final/com.example.task2_final.Bike}: java.lang.InstantiationException: can't instantiate class com.example.task2_final.Bike; no empty constructor
03-11 15:33:59.695: E/AndroidRuntime(1200): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
03-11 15:33:59.695: E/AndroidRuntime(1200): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)

这是我的 Bike.java 类:

package com.example.task2_final;


public class Bike {

private String title;
private String imageName;


//Constructor
Bike (String title, String imageName) {
this.title= title;
this.imageName = imageName;
}

public void setTitle(String title){
this.title = title;
}

public void setImage(String imageName){
this.imageName = imageName;
}
public String getBikeTitle(){
return title;
}

public String getBikeImageName(){
return imageName;
}



}

如果需要,这里是我的 AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.task2_final"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="19"
android:targetSdkVersion="19" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Bike"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

添加构造函数后的新错误:

03-11 15:42:21.735: E/AndroidRuntime(1247): FATAL EXCEPTION: main
03-11 15:42:21.735: E/AndroidRuntime(1247): Process: com.example.task2_final, PID: 1247
03-11 15:42:21.735: E/AndroidRuntime(1247): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.task2_final/com.example.task2_final.Bike}: java.lang.IllegalAccessException: access to constructor not allowed

使用自定义适配器.java

package com.example.task2_final;

import java.util.ArrayList;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class UseCustomAdapter extends ListActivity {

ArrayList<Bike> bikes = new ArrayList<Bike>();

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bikes.add(new Bike("Honda", "honda"));
bikes.add(new Bike("Suzuki", "suzuki"));
bikes.add(new Bike("Kawasaki", "kawasaki"));
bikes.add(new Bike("KTM", "ktm"));
CustomAdapter adapter = new CustomAdapter(this, bikes);
setListAdapter(adapter);

}

protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Bike b = bikes.get(position);
Toast.makeText(this,"You selected the " + b.getBikeTitle()
+ "Motocross Bike",Toast.LENGTH_SHORT).show();
}

}

最佳答案

你的问题出在AndroidManifest

<activity
android:name=".Bike"
android:label="@string/app_name" >

您正在将 Bike 声明为 Activity ,但 Bike 并未扩展 Activity。有趣的是,您并没有像预期的那样遇到编译时错误,而是运行时崩溃,它告诉您该类正在安装一个运行时通过反射。要修复它,要么让 Bike 扩展 Activity(并摆脱构造函数),要么将其从 Manifest 中删除。请注意,在扩展 Activity 的类上使用 new 运算符是一种不好的做法

关于java - 使用自定义适配器创建列表时出现“无空构造函数”logcat 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28995848/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com