Java 缺少零参数构造函数
我对编码还不熟悉,所以我对自己应该做什么感到困惑Java 缺少零参数构造函数,java,android,Java,Android,我对编码还不熟悉,所以我对自己应该做什么感到困惑 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 an
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告诉我需要包含一个零参数构造函数。我在想我该怎么做。我将感谢任何帮助
错误消息是:
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
java.lang.RuntimeException:无法实例化活动组件信息{com.example.kuriustry2/com.example.kuriustry2.CustomInfoWindowAdapter}:java.lang.InstanceException:java.lang.Class没有零参数构造函数
您不应该为您的逻辑使用活动/片段构造函数。对于您的方法,有很多方法,例如onCreate
,onStart
如果从类中删除此部分,则问题将得到解决,因此将生成默认的构造函数
,而不带参数
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);
}
但是如果你想在构造函数中注入一些东西,请阅读。你不应该像这样创建activities类
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);
}