在打开活动期间应用程序崩溃

时间:2019-12-22 15:32:10

标签: java android android-activity onitemclicklistener

打开活动时我的应用程序崩溃。

名为“意大利面”的活动代码

2020
2019
2018
2017
2016
2015
2014
2013
2012
2011
2010
2009
2008
2007
2006
2005
2004
2003
2002
2001
2000
1999
1998
1997
1996
1995
1994
1993
1992
1991
1990
1989
1988
1987
1986
1985
1984
1983
1982
1981
1967

导致活动崩溃的错误

package com.example.cook4u;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.content.Intent;



public class Pasta extends AppCompatActivity {

SearchView mysearchview3 = findViewById(R.id.searchView3);
String[] items = new String[]{"Masala pasta", "White sauce pasta", "Penne Alla Vodka", "Pasta 
Pomodoro", "Red Sauce Pasta", "Cheesezy Pasta", "Ravioli with Tomato Sauce", "Spaghetti Grilled 
Green Beans with Mushrooms", "Cheesy Shells and Greens", "Mac and Cheese", "5- Ingredient Pasta", 
"Creamy Cheddar Corn Pasta"};

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

    ListView listView = findViewById(R.id.listview3);
    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
android.R.layout.simple_list_item_1, items);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (position == 0) {
                    Intent myIntent;
                    myIntent = new Intent(Pasta.this,Masalapasta.class);
                    startActivity(myIntent);
                }
            if (position == 1) {
                Intent myIntent;
                myIntent = new Intent(Pasta.this,Whitesaucepasta.class);
                startActivity(myIntent);
            }
            if (position == 2) {
                Intent myIntent;
                myIntent = new Intent(Pasta.this,PenneAllaVodka.class);
                startActivity(myIntent);
            }
            if (position == 3) {
                Intent myIntent;
                myIntent = new Intent(Pasta.this,PastaPomodoro.class);
                startActivity(myIntent);
            }
            if (position == 4) {
                Intent myIntent;
                myIntent = new Intent(Pasta.this,RedSaucePasta.class);
                startActivity(myIntent);
            }
            if (position == 5) {
                Intent myIntent;
                myIntent = new Intent(Pasta.this,CheesezyPasta.class);
                startActivity(myIntent);
            }
            if (position == 6) {
                Intent myIntent;
                myIntent = new Intent(Pasta.this,Ravioli.class);
                startActivity(myIntent);
            }
            if (position == 7) {
                Intent myIntent;
                myIntent = new Intent(Pasta.this,Spaghetti.class);
                startActivity(myIntent);
            }
            if (position == 8) {
                Intent myIntent;
                myIntent = new Intent(Pasta.this,CheesyShellsandGreens.class);
                startActivity(myIntent);
            }
            if (position == 9) {
                Intent myIntent;
                myIntent = new Intent(Pasta.this,MacandCheese.class);
                startActivity(myIntent);
            }
            if (position == 10) {
                Intent myIntent;
                myIntent = new Intent(Pasta.this,IngredientPasta.class);
                startActivity(myIntent);
            }
            if (position == 11) {
                Intent myIntent;
                myIntent = new Intent(Pasta.this,CreamyCheddarCornPasta.class);
                startActivity(myIntent);
            }

        }
    });
    mysearchview3.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            adapter.getFilter().filter(newText);
            return false;
        }
    });
}
}

过程957终止。 处理957终止。 处理957终止。 处理957终止。 处理957终止。 处理957终止。 处理957终止。 进程957终止。

有人可以告诉我如何解决这个问题吗?

2 个答案:

答案 0 :(得分:2)

替换:

SearchView mysearchview3 = findViewById(R.id.searchView3);

具有:

SearchView mysearchview3;

然后在onCreate()的{​​{1}}中,添加:

setContentView(R.layout.activity_pasta);

在调用mysearchview3 = findViewById(R.id.searchView3); 后,您才能成功调用findViewById(),并且小部件将实际存在。直到您进行super.onCreate()调用之后,该窗口小部件才会存在。

许多关于Android应用程序开发的书籍和课程都涉及到这种情况。

答案 1 :(得分:0)

您在onCreate函数上方的代码将它们放在onCreate()中。这是因为项目的ID仅在创建视图时才呈现。

public class Pasta extends AppCompatActivity {
private SearchView mysearchview3;
private String[] items;

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

 mysearchview3 = findViewById(R.id.searchView3);
 items = new String[]{
"Masala pasta", 
"White sauce pasta", 
 "Penne Alla Vodka", 
"Pasta Pomodoro", 
"Red Sauce Pasta", 
"Cheesezy Pasta", 
"Ravioli with Tomato Sauce", 
"Spaghetti Grilled  Green Beans with Mushrooms", 
"Cheesy Shells and Greens", 
"Mac and Cheese", 
"5- Ingredient Pasta", 
"Creamy Cheddar Corn Pasta"};
}
相关问题