简体   繁体   中英

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. I was wondering how I would go about doing this. 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. For your method there are a lot of method such as onCreate , onStart ...

Your problem will fix if you delete this part from the class so there will be generated default constructor without arguments

    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 .

you shouldn't create activities class like this

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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