gpt4 book ai didi

由于 java.lang.NullPointerException,Android 应用程序在启动时崩溃

转载 作者:行者123 更新时间:2023-11-29 00:48:51 24 4
gpt4 key购买 nike

这是我的 .java 文件:

package com.shoppinglist;

import java.util.ArrayList;

import android.app.Dialog;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class ShoppingList extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final EditText et = (EditText) findViewById(R.id.edittext);
ListView lv = (ListView) findViewById(android.R.id.list);
final ArrayList<String> items = new ArrayList<String>();
final ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, items);
lv.setAdapter(adapter);
setContentView(R.layout.main);

Button button1main = (Button) findViewById(R.id.add);
button1main.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = new Dialog(ShoppingList.this);
dialog.setContentView(R.layout.maindialog);
dialog.setCancelable(true);

Button button = (Button) dialog.findViewById(R.id.cancel);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();

Button ok = (Button) dialog.findViewById(R.id.ok);
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
items.add(0, et.getText().toString());
adapter.notifyDataSetChanged();
dialog.dismiss();
et.setText("");
}

}
);
}
});
}
}

最佳答案

添加

setContentView(R.layout.main);

之后

super.onCreate(savedInstanceState);

这将解决您的问题。

关于由于 java.lang.NullPointerException,Android 应用程序在启动时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4706894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com