越来越奇怪的字符串崩溃

时间:2018-11-13 05:39:36

标签: android

因此,基本上,当我启动程序时,它可以正常工作,但是当我按“后退”按钮返回到上一个活动时,它几乎立即崩溃。我已经使用调试工具进行了检查,它可以完成我的onActivity Result方法。

错误日志

  2018-11-13 00:37:48.874 8349-8349/com.example.caesp.todolist E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.caesp.todolist, PID: 8349
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:445)
        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:407)
        at android.widget.AbsListView.obtainView(AbsListView.java:3189)
        at android.widget.ListView.makeAndAddView(ListView.java:2197)
        at android.widget.ListView.fillDown(ListView.java:824)
        at android.widget.ListView.fillSpecific(ListView.java:1534)
        at android.widget.ListView.layoutChildren(ListView.java:1914)
        at android.widget.AbsListView.onLayout(AbsListView.java:2961)
        at android.view.View.layout(View.java:20836)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.support.constraint.ConstraintLayout.onLayout(ConstraintLayout.java:1915)
        at android.view.View.layout(View.java:20836)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20836)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444)
        at android.view.View.layout(View.java:20836)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at android.view.View.layout(View.java:20836)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1791)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1635)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1544)
        at android.view.View.layout(View.java:20836)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
        at com.android.internal.policy.DecorView.onLayout(DecorView.java:944)
        at android.view.View.layout(View.java:20836)
        at android.view.ViewGroup.layout(ViewGroup.java:6401)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2948)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2635)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1779)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7810)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:658)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6938)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

现在,我的目标是仅解决崩溃问题。

对不起,我忘记添加实际的代码。

package com.example.caesp.todolist;


import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


    ListView ListofList;
    ArrayList<String> Lists;
    ArrayAdapter<String> ListAdapter;
    Button button;
    ArrayList<String> Subject = new ArrayList<>();
    ArrayList<String> tit = new ArrayList<>();
    int Size;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListofList = findViewById(R.id.LofL);
        Lists = new ArrayList<String>();
        ListAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Lists);
        ListofList.setAdapter(ListAdapter);

        button = findViewById(R.id.AddList);
        Lists.add("Test");

        ListofList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent i = new Intent(getBaseContext(), ItemList.class);
                String s = (String) ListofList.getItemAtPosition(position);
                i.putExtra("Title", s);
                i.putExtra("size", Size);
                int a = 1;
                for (String t : Subject) {
                    i.putExtra("Item" + a, t);
                    a++;
                }
                a = 1;
                for (String u : tit) {
                    i.putExtra("title" + a, u);
                    a++;
                }
                startActivityForResult(i, 2);
            }
        });

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getBaseContext(), CreateItem.class);
                String message = "Please enter the name of the list below.";
                i.putExtra("mess", message);
                startActivityForResult(i, 1);
            }
        });



    }

    @Override
    protected void onActivityResult (int requestCode, int resultCode, Intent data) {
        Bundle extras = getIntent().getExtras();

        if (data == null) {

        }
        if (data != null) {
            String item = (data.getStringExtra("item"));
            Lists.add(item);
            ListAdapter.notifyDataSetChanged();
        }

        if (requestCode == 2) {
            ArrayList<String> subject = new ArrayList<String>();
            ArrayList<String> title = new ArrayList<String>();
            int i = 1;
            assert data != null;
            int size = (data.getIntExtra("ListSize", 0));
            Size = size;
            while (i <= size) {
                String item = (data.getStringExtra("item" + i));
                subject.add(item);

                i++;
            }
            i = 1;
            while (i <= size) {
                String item = (data.getStringExtra("title" + i));
                title.add(item);

                i++;
            }
            Subject = subject;
            tit = title;
        }
    }


}

1 个答案:

答案 0 :(得分:0)

我发现我的代码有问题。从单独的活动返回后,onActivityResult在以下位置创建了空值

if (data != null) {
            String item = (data.getStringExtra("item"));
            Lists.add(item);
            ListAdapter.notifyDataSetChanged();
        }

由于没有Extra称为“ item”,因此它创建了一个空值并将其添加到ListAdapter。抱歉,如果您这样做,那会浪费您的时间。