简体   繁体   English

修改数组时,应用程序崩溃<ObjectType>

[英]android - Application crashes upon modifying ArrayList<ObjectType>

I'm working on an application which will have a ListView alongwith a Button to add items to the ListView dynamically. 我正在开发一个具有ListView一个将Button动态添加到ListViewButton的应用程序。 I've made a custom adapter for the ListView . 我为ListView制作了一个自定义适配器。 My idea is when the user will tap the Button it'll show an AlertDialog with a TextView and two buttons, "OK" and "Cancel" . 我的想法是,当用户点击Button它将显示一个带有TextView和两个按钮的AlertDialog ,分别是“ OK”“ Cancel” This my activity where the ListView will be shown - 这是我在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;
    }
}

But, the whole application crashes when I tap the "OK" button. 但是,当我点击“确定”按钮时,整个应用程序崩溃了。 It works fine if I omit the_subjects.add(new topic(new_topic_name)); 如果我省略the_subjects.add(new topic(new_topic_name)); . But then the newly added item disappears when I reload the activity. 但是,当我重新加载活动时,新添加的项目消失了。

Here is my custom ListView adapter - 这是我的自定义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 - 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 - topic.java-

public class topic { 公开课主题{

String Topic;

public topic(String topic) {
    Topic = topic;
}
}

Here's the log of my error - 这是我的错误日志-

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)

I can't figure out figure out how to modify the ArrayList<topic> without crashing the app. 我无法弄清楚如何在不使应用程序崩溃的情况下修改ArrayList<topic> Please help me to solve this problem. 请帮我解决这个问题。 Thanks in advance!! 提前致谢!!

You will replace your code...Your Code is 您将替换您的代码...您的代码是

            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();

You don't want to set null for your adapter So you will use 您不想为适配器设置null,因此您将使用

          adapter.clear();

It will delete your whole data in your adapter Just replace with that single line 它将删除适配器中的全部数据,只需替换为那一行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM