简体   繁体   English

如果我尝试进行特定活动,则应用程序崩溃

[英]App crashes if I try going to a specific activity

So I am making a simple app. 所以我正在做一个简单的应用程序。 It's just basically making a list of win with a custom list view at the end. 基本上,它只是通过最后的自定义列表视图来制作获胜列表。

It starts off on the main screen where there are two buttons, one is an "Add" button which takes you to the Add activity. 它从主屏幕上开始,那里有两个按钮,一个是“添加”按钮,可将您带到“添加”活动。 If you push this it'll take you to a page where you type in the name,price, description of the wine and then hit a submit button to the list. 如果按下此按钮,将带您到一个页面,在其中键入葡萄酒的名称,价格,描述,然后在列表中单击“提交”按钮。 The other button on the main screen is a "Go to List" button which just takes you directly to the list activity. 主屏幕上的另一个按钮是“转到列表”按钮,它直接将您带到列表活动。

If I go through the Add button, add a wine, and then go to the list, it works fine. 如果我单击“添加”按钮,添加一种葡萄酒,然后转到列表,则效果很好。 I can see the list. 我可以看到清单。 It even works if I don't add anything to the list. 如果我不添加任何内容,它甚至可以工作。 I can see the empty list activity. 我可以看到空列表活动。

When I push the "Go to List" button on the main screen though, it crashes and says "The application has stopped". 但是,当我按下主屏幕上的“转到列表”按钮时,它崩溃并显示“应用程序已停止”。

I don't get why I can go through the Add button to get to the list fine, but this button doesn't work at all. 我不明白为什么我可以通过“添加”按钮进入列表,但是此按钮根本不起作用。

Could I get some help? 我可以帮忙吗?

Here are the three relevant activities, the AddActivity, the ListActivity, and the MainActivity. 这是三个相关的活动,AddActivity,ListActivity和MainActivity。

AddActivity: AddActivity:

public class AddActivity extends AppCompatActivity {


EditText editWineName;
EditText editWinePrice;
EditText editWineDescription;
Button btnSubmit;
Button btnGoToList;
String stringWineName;
String stringWinePrice;
String stringWineDescription;
ArrayList<String> listWineName = new ArrayList<>();
ArrayList<String> listPrice = new ArrayList<>();
ArrayList<String> listWineDescription = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add);
    setVariables();



    btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setVariables();
            listWineName.add(stringWineName);
            listPrice.add(stringWinePrice);
            listWineDescription.add(stringWineDescription);
            Toast.makeText(AddActivity.this, stringWineName + " was added to the list.", Toast.LENGTH_SHORT).show();
            clearEditText();

        }
    });

    btnGoToList.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intentGoToList = new Intent(AddActivity.this,ListActivity.class);
            intentGoToList.putStringArrayListExtra("WINENAME", listWineName);
            intentGoToList.putStringArrayListExtra("WINEPRICE", listPrice);
            intentGoToList.putStringArrayListExtra("WINEDESCRIPTION", listWineDescription);
            startActivity(intentGoToList);
        }
    });

}

private void setVariables(){
    editWineName = (EditText) findViewById(R.id.editName);
    editWinePrice = (EditText) findViewById(R.id.editPrice);
    editWineDescription = (EditText) findViewById(R.id.editDescription);
    btnSubmit = (Button) findViewById(R.id.btnSubmit);
    btnGoToList = (Button) findViewById(R.id.btnGoToList);
    stringWineName = editWineName.getText().toString();
    stringWinePrice = "$" + editWinePrice.getText().toString();
    stringWineDescription = editWineDescription.getText().toString();
}

private void clearEditText() {
    editWineName.getText().clear();
    editWinePrice.getText().clear();
    editWineDescription.getText().clear();
}
}

ListActivity: ListActivity:

public class ListActivity extends AppCompatActivity {

ListView wineList;
ArrayAdapter adapter;
Button btnBacktoMain;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    setVariables();



    ArrayList<String> listWineName = getIntent().getStringArrayListExtra("WINENAME");
    ArrayList<String > listWinePrice = getIntent().getStringArrayListExtra("WINEPRICE");
    ArrayList<String> listWineDescription = getIntent().getStringArrayListExtra("WINEDESCRIPTION");

    adapter = new CustomAdapter(this, listWineName, listWinePrice, listWineDescription);
    wineList.setAdapter(adapter);


    btnBacktoMain.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intentBackToMain = new Intent(ListActivity.this,MainActivity.class);
            startActivity(intentBackToMain);
        }
    });

}

private void setVariables (){
    btnBacktoMain = (Button) findViewById(R.id.btnBackToMain);
    wineList = (ListView) findViewById(R.id.listWine);

}

} }

MainActivity: 主要活动:

public class MainActivity extends AppCompatActivity {

Button btnAdd;
Button btnList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setVariables();

    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {                                                //Goes to the add activity
            Intent intentAdd = new Intent(MainActivity.this, AddActivity.class);
            startActivity(intentAdd);
        }
    });

    btnList.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {                                              //Goes to the list activity
            Intent intentList = new Intent(MainActivity.this, ListActivity.class);
            startActivity(intentList);
        }
    });
}

private void setVariables(){
    btnAdd = (Button) findViewById(R.id.btnAddWine);
    btnList = (Button) findViewById(R.id.btnViewList);
}

} }

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
    at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:344)
    at android.widget.ListView.setAdapter(ListView.java:493)
    at com.example.jeremy.mywine.ListActivity.onCreate(ListActivity.java:33)

Your crash indicates that the data in the adapter given to the ListView in ListActivity is null. 您的崩溃表明在ListActivity中提供给ListView的适配器中的数据为空。 Make it not null. 使其不为空。 Start at ListActivity.java at line 33 and go backwards to find where you are (or this case are not) initializing the data in the list adapter. 从第33行的ListActivity.java开始,然后向后查找您正在(或在这种情况下不是)初始化列表适配器中的数据的位置。

In you case, you are expecting data in your intent. 在这种情况下,您期望有意图的数据。 OK, where is your intent set up? 好,您的意图在哪里设置? In your MainActivity click. 在您的MainActivity中,单击。 Well, there you just launch the activity without passing any data in the intent extras, hence there is nothing to pull out from the intent in ListActivity , hence your crash. 好的,您只启动了活动而没有在意向附加中传递任何数据,因此ListActivity的意向没有任何可撤出的ListActivity ,因此会导致崩溃。

So you need to initialize the data in MainActivity and set it as extras in the Intent you use to launch ListActivity . 因此,您需要在MainActivity初始化数据并将其设置为用于启动ListActivity的Intent中的额外数据。

Since the ListActivity is expecting this: 由于ListActivity期望这样:

ArrayList<String> listWineName = getIntent().getStringArrayListExtra("WINENAME");
ArrayList<String > listWinePrice = getIntent().getStringArrayListExtra("WINEPRICE");
ArrayList<String> listWineDescription = getIntent().getStringArrayListExtra("WINEDESCRIPTION");

You would update your MainActivity to do something like this (where getDescriptions() is a fictitious method you would create to return a list of strings) 您将更新MainActivity以执行类似的操作(其中getDescriptions()是一种创建返回字符串列表的虚构方法)

btnList.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        //Goes to the list activity
        Intent intentList = new Intent(MainActivity.this, ListActivity.class);
        intentList.putExtra("WINENAME", new ArrayList<String>());     // Empty list
        intentList.putExtra("WINEPRICE", Arrays.asList("Foo", "Bar")); // Explicit list named items
        intentList.putExtra("WINEDESCRIPTION", getDescriptions());    // Get list from private method
        startActivity(intentList);
    }
});

Also check this post my be useful for learning how to read and understand a crash log. 还要检查这篇文章对学习如何阅读和理解崩溃日志很有用。

And check the documentation on how to start activities . 查看有关如何开始活动的文档

Hope that helps! 希望有帮助!

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

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