首页 > 解决方案 > 如何在 Android Java 中定义多重继承

问题描述

我的 manig=fest 中有两个我不明白的错误,希望有人能帮助我。我的清单中的错误都是与活动相关的错误:<activity android:name=".PersonListAdapter"/>

错误是:

  1. 它的类应该提供一个默认构造函数(一个没有参数的公共构造函数)
  2. PersonListAdapter 必须扩展 android.app.Activity - 关于此错误:如何定义该类从另一个类和 Android 应用程序扩展?

这是我的代码:
清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="co.il.applicationmoran">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".register">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />


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

        <activity android:name=".PersonListAdapter"/>

        <activity android:name=".login"/>

    </application>

</manifest>

班级

package co.il.applicationmoran;

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

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

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

public class PersonListAdapter  extends ArrayAdapter<person>  {
    private static String TAG = "PersonListAdapter";
    private Context mycontext;
    int myresource;

    public PersonListAdapter(Context context, int resource, ArrayList<person> objects) {
        super(context, resource, objects);
        this.mycontext = context;
        this.myresource=resource;
    }

    @NonNull
    @Override
    public View getView(int position,  View convertView,  ViewGroup parent) {
        String name = getItem(position).getName();
        String age=getItem(position).getAge();
        String sex=getItem(position).getSex();

        person person=new person(name,age,sex);
        LayoutInflater inflater=LayoutInflater.from(mycontext);
        convertView=inflater.inflate(myresource, parent, false);

        TextView tvName=(TextView) convertView.findViewById(R.id.textview1);
        TextView tvAge=(TextView) convertView.findViewById(R.id.textview2);
        TextView tvSex=(TextView) convertView.findViewById(R.id.textview3);

        tvName.setText(name);
        tvAge.setText(age);
        tvSex.setText(sex);

        return convertView;
    }
}

标签: javaandroid

解决方案


  1. 可以将不带参数的公共构造函数添加为:

公共 PersonListAdapter() { }

  1. 适配器类不需要在清单文件中提及,您主要在清单中提及活动和服务类。从清单文件中删除以下行:
<activity android:name=".peoplefinder"/> //remoe if this is also not an activity class

<activity android:name=".PersonListAdapter"/> //remove

<activity android:name=".login"/> //remove if this is also not an activity class

推荐阅读