java - 打开 Activity 期间应用程序崩溃

标签 java android android-activity onitemclicklistener

我的应用程序在打开 Activity 时崩溃。

名为pasta的 Activity 代码

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

导致 Activity 崩溃的错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cook4u, PID: 957
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.cook4u/com.example.cook4u.Pasta}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2580)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6165)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 
  'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
    at android.support.v7.app.AppCompatDelegateImpl.<init>(AppCompatDelegateImpl.java:249)
    at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
    at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
    at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
    at com.example.cook4u.Pasta.<init>(Pasta.java:17)
    at java.lang.Class.newInstance(Native Method)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1100)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2570)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2743) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1490) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6165) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778) 

进程 957 终止。 进程 957 终止。 进程 957 终止。 进程 957 终止。 进程 957 终止。 进程 957 终止。 进程 957 终止。 进程 957 终止。

谁能告诉我如何解决这个问题?

最佳答案

替换:

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

与:

SearchView mysearchview3;

然后,在onCreate()中,在setContentView(R.layout.activity_pasta);之后,添加:

mysearchview3 = findViewById(R.id.searchView3);

在调用 super.onCreate() 且小部件实际存在之前,您无法成功调用 findViewById()该小部件在您调用 setContentView() 之前不会存在。

许多有关 Android 应用程序开发的书籍和类(class)都介绍了此类内容。

关于java - 打开 Activity 期间应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59445581/

相关文章:

java - Android 如何了解用户正在查看哪个主屏幕?

java - 将同级项目资源添加到 Android 资源中,无需紧密耦合

java - jsp jdbc数据访问层设计

database - 从 CSV 文件填充 Android 数据库?

java - 如何从 SurfaceView 类或嵌套线程中结束 Activity

android - 在文件选择器中访问外部存储

java - bindService android AIDL 总是返回 false

java - 循环并将 JSON 对象转换为数组

android - monkeyrunner 脚本 - 启动需要权限的 Activity

android - 如何在没有提示的情况下以编程方式安装 android 应用程序,