我正在处理一个应用程序,该应用程序将ListView
与Button
一起动态地向ListView
添加项目。我为ListView
制作了一个自定义适配器。我的想法是,当用户点按Button
时,它会显示AlertDialog
TextView
和两个按钮,“确定”和“取消“即可。这是我将展示ListView
的活动 -
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Add_Topics extends Activity {
AddTopics_MenuAdapter adapter;
ListView topics_list;
public ArrayList<topic> the_subjects;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add__topics);
the_subjects = Model.loadTopics();
topics_list = (ListView) findViewById(R.id.topics_list);
overridePendingTransition(R.anim.reverse_in_left, R.anim.reverse_out_left);
Animation enter = AnimationUtils.loadAnimation(getBaseContext(), R.anim.upcoming_menu);
Animation enter_slow = AnimationUtils.loadAnimation(getBaseContext(), R.anim.enter_l2r_slide);
TextView des = (TextView)findViewById(R.id.des_at);
TextView title = (TextView)findViewById(R.id.title_at);
Button add_topic = (Button)findViewById(R.id.add_topic_button);
Typeface roboto_lt = Typeface.createFromAsset(getAssets(), "Roboto-Light.ttf");
des.setTypeface(roboto_lt);
title.setTypeface(roboto_lt);
add_topic.setTypeface(roboto_lt);
title.startAnimation(enter);
des.startAnimation(enter_slow);
adapter = new AddTopics_MenuAdapter(this, the_subjects);
topics_list.setAdapter(adapter);
}
public void onClickAddTopic(View v) {
showDialog(0);
}
protected Dialog onCreateDialog(int id) {
switch(id) {
case 0:
LayoutInflater li = LayoutInflater.from(this);
View edt_txt_add_tpc = li.inflate(R.layout.add_topics_res, null);
edt_txt_add_tpc.requestFocus();
final EditText tpc_nm = (EditText) edt_txt_add_tpc.findViewById(R.id.topic_name);
Builder bld = new AlertDialog.Builder(this);
bld.setIcon(R.drawable.ic_launcher);
bld.setTitle("Add Topic/Subject");
bld.setView(edt_txt_add_tpc);
bld.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButon) {
String new_topic_name = tpc_nm.getText().toString();
adapter = null;
the_subjects.add(new topic(new_topic_name));
adapter = new AddTopics_MenuAdapter(getBaseContext(), the_subjects);
adapter.notifyDataSetChanged();
}
});
bld.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
return bld.create();
}
return null;
}
@Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_add__topics, menu);
return true;
}
}
但是,当我点按“确定”按钮时,整个应用程序崩溃了。如果省略the_subjects.add(new topic(new_topic_name));
,它可以正常工作。但是当我重新加载活动时,新添加的项目就会消失。
这是我的自定义ListView
适配器 -
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class AddTopics_MenuAdapter extends ArrayAdapter<topic> {
private final Context context;
final ArrayList<topic> topics;
public AddTopics_MenuAdapter(Context context, ArrayList<topic> topics) {
super(context, R.layout.add_topics_menu, topics);
this.context = context;
this.topics = topics;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View topicsView = inflater.inflate(R.layout.add_topics_menu, parent, false);
TextView topic_name = (TextView) topicsView.findViewById(R.id.topic_title);
Typeface rbt_lt = Typeface.createFromAsset(getContext().getAssets(), "Roboto-Light.ttf");
Animation enter = AnimationUtils.loadAnimation(getContext(), R.anim.upcoming_menu);
topic_name.setText(topics.get(position).toString());
topic_name.setTypeface(rbt_lt);
topic_name.setAnimation(enter);
return topicsView;
}
}
Model.java -
import java.util.ArrayList;
import android.util.Log;
import android.widget.Toast;
public class Model {
public static ArrayList<topic> list_of_topics;
public static String[] subjects;
public static ArrayList<topic> loadTopics() {
list_of_topics = new ArrayList<topic>();
list_of_topics.add(new topic("History"));
list_of_topics.add(new topic("Geography"));
return list_of_topics;
}
}
topic.java -
公共课题{
String Topic;
public topic(String topic) {
Topic = topic;
}
}
这是我的错误记录 -
03-07 18:13:28.271: E/AndroidRuntime(20349): FATAL EXCEPTION: main
03-07 18:13:28.271: E/AndroidRuntime(20349): java.lang.NullPointerException
03-07 18:13:28.271: E/AndroidRuntime(20349): at com.Swap.StudyBuddy.Add_Topics$1.onClick(Add_Topics.java:80)
03-07 18:13:28.271: E/AndroidRuntime(20349): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:170)
03-07 18:13:28.271: E/AndroidRuntime(20349): at android.os.Handler.dispatchMessage(Handler.java:107)
03-07 18:13:28.271: E/AndroidRuntime(20349): at android.os.Looper.loop(Looper.java:194)
03-07 18:13:28.271: E/AndroidRuntime(20349): at android.app.ActivityThread.main(ActivityThread.java:5409)
03-07 18:13:28.271: E/AndroidRuntime(20349): at java.lang.reflect.Method.invokeNative(Native Method)
03-07 18:13:28.271: E/AndroidRuntime(20349): at java.lang.reflect.Method.invoke(Method.java:525)
03-07 18:13:28.271: E/AndroidRuntime(20349): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-07 18:13:28.271: E/AndroidRuntime(20349): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
03-07 18:13:28.271: E/AndroidRuntime(20349): at dalvik.system.NativeStart.main(Native Method)
我无法弄清楚如何在不崩溃应用程序的情况下修改ArrayList<topic>
。请帮我解决这个问题。在此先感谢!!
答案 0 :(得分:0)
您将替换您的代码......您的代码是
String new_topic_name = tpc_nm.getText().toString();
adapter = null;
the_subjects.add(new topic(new_topic_name));
adapter = new AddTopics_MenuAdapter(getBaseContext(), the_subjects);
adapter.notifyDataSetChanged();
您不希望为适配器设置null所以您将使用
adapter.clear();
它将删除适配器中的整个数据只需用该单行替换