0
我正在處理的應用程序將有一個ListView
以及Button
動態添加項目到ListView
。我爲ListView
製作了一個自定義適配器。我的想法是,當用戶點擊Button
它會顯示一個AlertDialog
與TextView
和兩個按鈕,「OK」和「取消」。這是我的活動,其中ListView
將顯示 -android-應用程序崩潰後修改ArrayList <ObjectType>
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;
}
}
但是,整個應用程序崩潰時我挖掘「OK」按鈕。如果我省略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>
沒有崩潰的應用程序。請幫我解決這個問題。提前致謝!!
發佈堆棧跟蹤和主題類 – Raghunandan
我已經更新了問題,現在它包括崩潰的日誌。 – Swap
「Add_Topics.java」中的第80行是什麼? – Raghunandan