簡體   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