简体   繁体   English

缺少零参数构造函数

[英]Missing Zero Argument Constructor

I'm new to coding so I'm confused as to what I'm supposed to do.我是编码新手,所以我对我应该做什么感到困惑。

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;
    }
}

That is my code and logcat tells me that I need to include a zero argument constructor.那是我的代码,logcat 告诉我需要包含一个零参数构造函数。 I was wondering how I would go about doing this.我想知道我将如何 go 这样做。 I would appreciate any help.我将不胜感激任何帮助。

The error message was:错误消息是:

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

You should not use Activity/Fragment constructor for yours logic.您不应该为您的逻辑使用 Activity/Fragment constructor For your method there are a lot of method such as onCreate , onStart ...对于您的方法,有很多方法,例如onCreateonStart ...

Your problem will fix if you delete this part from the class so there will be generated default constructor without arguments如果您从 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 but if you want to inject something in constructer read this . PS 但如果你想在构造函数中注入一些东西,请阅读这个

you shouldn't create activities class like this你不应该像这样创建活动 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);
    }

write this way这样写

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