繁体   English   中英

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

[英]'no empty constructor' logcat error when creating list with a custom adapter

我正在尝试使用自定义适配器填充列表,但是当我尝试运行该应用程序时,它只会崩溃。 检查我的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

UseCustomAdapter.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声明为活动,但Bike并未扩展Activity 有趣的事实是,您不会像预期的那样收到编译时错误 ,而是运行时崩溃 ,它告诉您该类正在实例化运行时反射。 要解决此问题,请让Bike扩展Activity(并摆脱构造函数)或将其从清单中删除。 请注意,在扩展Activity的类上使用new运算符是一种不良做法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM