繁体   English   中英

缺少零参数构造函数

[英]Missing Zero Argument Constructor

我是编码新手,所以我对我应该做什么感到困惑。

package com.example.kuriustry2;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.maps.model.Marker;

public class CustomInfoWindowAdapter extends AppCompatActivity {

    public final View mWindow;
    private final Context mContext;

    public CustomInfoWindowAdapter(Context context) {
        Toast.makeText(getApplicationContext(),"yeet",Toast.LENGTH_SHORT).show();
        mContext = context;
        mWindow = LayoutInflater.from(context).inflate(R.layout.custom_info_window,null);
    }

    private void rendowWindowText(View view, Intent intent) {

        Bundle extras = getIntent().getExtras();
        String foodbank = intent.getStringExtra("FOOD_BANK");
        String address = intent.getStringExtra("STREET_ADDRESS");
        String website = intent.getStringExtra("WEBSITE");

        //Intent intent = getIntent();
        //String foodBank = intent.getStringExtra("FOOD_BANK");
        TextView tvTitle = (TextView) view.findViewById(R.id.title);
        tvTitle.setText(foodbank);

        //String snippet = marker.getSnippet();
        TextView tvSnippet = (TextView) view.findViewById(R.id.snippet);
        //String address = getStringExtra("STREET_ADDRESS");
        tvSnippet.setText(address);
    }

    public View getInfoWindow(Intent intent) {
        rendowWindowText(mWindow,intent);
        return mWindow;
    }

    public View getInfoContents(Intent intent) {
        rendowWindowText(mWindow,intent);
        return mWindow;
    }
}

那是我的代码,logcat 告诉我需要包含一个零参数构造函数。 我想知道我将如何 go 这样做。 我将不胜感激任何帮助。

错误消息是:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.kuriustry2/com.example.kuriustry2.CustomInfoWindowAdapter}: java.lang.InstantiationException: java.lang.Class<com.example.kuriustry2.CustomInfoWindowAdapter> has no zero argument constructor

您不应该为您的逻辑使用 Activity/Fragment constructor 对于您的方法,有很多方法,例如onCreateonStart ...

如果您从 class 中删除此部分,您的问题将得到解决,因此将生成没有 arguments 的默认constructor函数

    public CustomInfoWindowAdapter(Context context) {
        Toast.makeText(getApplicationContext(),"yeet",Toast.LENGTH_SHORT).show();
        mContext = context;
        mWindow = LayoutInflater.from(context).inflate(R.layout.custom_info_window,null);
    }

PS 但如果你想在构造函数中注入一些东西,请阅读这个

你不应该像这样创建活动 class

public class CustomInfoWindowAdapter extends AppCompatActivity {

    public final View mWindow;
    private final Context mContext;

    public CustomInfoWindowAdapter(Context context) {
        Toast.makeText(getApplicationContext(),"yeet",Toast.LENGTH_SHORT).show();
        mContext = context;
        mWindow = LayoutInflater.from(context).inflate(R.layout.custom_info_window,null);
    }

这样写

public class CustomInfoWindowAdapter extends AppCompatActivity {

    public final View mWindow;


     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

setContentView(R.layout.custom_info_window);

}

暂无
暂无

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

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