简体   繁体   English

打开活动时应用程序崩溃

[英]app crashes when during opening an activity

My app crashes when opening an activity.打开活动时我的应用程序崩溃。

Code of activity named pasta名为面食的活动代码

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

error which causes the error of crashing the 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) 

Process 957 terminated.进程 957 终止。 Process 957 terminated.进程 957 终止。 Process 957 terminated.进程 957 终止。 Process 957 terminated.进程 957 终止。 Process 957 terminated.进程 957 终止。 Process 957 terminated.进程 957 终止。 Process 957 terminated.进程 957 终止。 Process 957 terminated.进程 957 终止。

Can anybody tell how can I solve this issue?谁能告诉我如何解决这个问题?

Replace:代替:

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

with:和:

SearchView mysearchview3;

Then, in onCreate() , after setContentView(R.layout.activity_pasta);然后,在onCreate() ,在setContentView(R.layout.activity_pasta);之后setContentView(R.layout.activity_pasta); , add: , 添加:

mysearchview3 = findViewById(R.id.searchView3);

You cannot call findViewById() successfully until after super.onCreate() has been called and the widget will actually exist.在调用super.onCreate()并且小部件实际存在之前,您无法成功调用findViewById() That widget will not exist until after your setContentView() call.在您调用setContentView()之后,该小部件才会存在。

This sort of thing is covered in many books and courses on Android app development.许多关于 Android 应用程序开发的书籍和课程都涵盖了这类事情。

Your code above the onCreate functions place them inside onCreate().It is because the id of an item is only rendered when your view is created. 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"};
}

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

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