无论何时打开“自定义对话框”,应用程序都会崩溃并引发以下错误
ive检查了重复的初始化,但找不到任何内容
在MainActivity.java
public class MainActivity extends AppCompatActivity {
PieChart pieChart;
ImageView edit;
Dialog editDialog;
EditText edit_cal,edit_carb,edit_prot,edit_fats;
String cal,carb,prot,fats;
Button save;
public static final String SHARED_PREFS = "sharedPrefs";
public static final String CAL = "text";
public static final String CARB = "text";
public static final String PROT = "text";
public static final String FATS = "text";
TextView tcal,tcarb,tprot,tfats;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit=findViewById(R.id.edit);
pieChart=findViewById(R.id.pie);
tcal=findViewById(R.id.tcal);
tcarb=findViewById(R.id.tcarb);
tprot=findViewById(R.id.tprot);
tfats=findViewById(R.id.tfats);
saveData();
loadData();
updateViews();
edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
custom_Dialog();
}
});
}
private void custom_Dialog() {
editDialog=new Dialog(MainActivity.this);
editDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
editDialog.setCancelable(true);
editDialog.setContentView(R.layout.edit_nutrient);
editDialog.show();
edit_cal=findViewById(R.id.edit_cal);
edit_carb=findViewById(R.id.edit_carb);
edit_prot=findViewById(R.id.edit_prot);
edit_fats=findViewById(R.id.edit_fats);
save=findViewById(R.id.savebtn);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cal=edit_cal.getText().toString();
carb=edit_carb.getText().toString();
prot=edit_prot.getText().toString();
fats=edit_fats.getText().toString();
if(isValid(cal,carb,prot,fats))
{
saveData();
loadData();
updateViews();
editDialog.dismiss();
}
else
{
Toast.makeText(MainActivity.this,"Please fill your diet plan correctly",Toast.LENGTH_SHORT).show();
}
}
});
}
在edit_ Nutrition.xml中
<Button
android:id="@+id/savebtn"
android:layout_width="296dp"
android:layout_height="48dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="340dp"
android:background="@drawable/button_default_2"
android:fontFamily="sans-serif-medium"
android:letterSpacing="-0.03"
android:text="Save"
android:textColor="#ffffff"
android:textSize="13sp"
android:textStyle="normal"
/>
android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference
at com.example.android.chartvisual.MainActivity.custom_Dialog(MainActivity.java:122)
at com.example.android.chartvisual.MainActivity.access$000(MainActivity.java:29)
at com.example.android.chartvisual.MainActivity$1.onClick(MainActivity.java:101)
at android.view.View.performClick(View.java:6319)
at android.view.View$PerformClick.run(View.java:24955)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6701)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
答案 0 :(得分:1)
在这里,您设置对话框的内容视图:
editDialog.setContentView(R.layout.edit_nutrient);
但是,在以下几行中,您是在“活动”而不是对话框中搜索视图:
edit_cal=findViewById(R.id.edit_cal); edit_carb=findViewById(R.id.edit_carb); edit_prot=findViewById(R.id.edit_prot); edit_fats=findViewById(R.id.edit_fats); save=findViewById(R.id.savebtn);
您需要使用对话框的findViewById()
方法在其中进行搜索:
edit_cal=editDialog.findViewById(R.id.edit_cal);
edit_carb=editDialog.findViewById(R.id.edit_carb);
edit_prot=editDialog.findViewById(R.id.edit_prot);
edit_fats=editDialog.findViewById(R.id.edit_fats);
save=editDialog.findViewById(R.id.savebtn);
答案 1 :(得分:0)
您必须从布局创建View
,然后将其传递给创建对话框。还可以使用视图实例获取子元素。检查以下内容:
private void custom_Dialog() {
View view = LayoutInflater.from(this).inflate(R.layout.edit_nutrient, null, false);
editDialog=new Dialog(MainActivity.this);
editDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
editDialog.setCancelable(true);
editDialog.setContentView(view);
editDialog.show();
edit_cal = view.findViewById(R.id.edit_cal);
edit_carb = view.findViewById(R.id.edit_carb);
edit_prot = view.findViewById(R.id.edit_prot);
edit_fats = view.findViewById(R.id.edit_fats);
save = view.findViewById(R.id.savebtn);
save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cal=edit_cal.getText().toString();
carb=edit_carb.getText().toString();
prot=edit_prot.getText().toString();
fats=edit_fats.getText().toString();
if(isValid(cal,carb,prot,fats))
{
saveData();
loadData();
updateViews();
editDialog.dismiss();
}
else
{
Toast.makeText(MainActivity.this,"Please fill your diet plan correctly",Toast.LENGTH_SHORT).show();
}
}
});
}