Reputation: 1
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. Process 957 terminated. Process 957 terminated. Process 957 terminated. Process 957 terminated. Process 957 terminated. Process 957 terminated. Process 957 terminated.
Can anybody tell how can I solve this issue?
Upvotes: 0
Views: 50
Reputation: 2202
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.
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"};
}
Upvotes: 0
Reputation: 1007266
Replace:
SearchView mysearchview3 = findViewById(R.id.searchView3);
with:
SearchView mysearchview3;
Then, in onCreate()
, after 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. That widget will not exist until after your setContentView()
call.
This sort of thing is covered in many books and courses on Android app development.
Upvotes: 2