Reputation: 9
I am attempting to implement a search view into my android application action bar (I will be using Parse to store the data). I have followed google's, and a few other tutorials I found online and it will not work. Every time I attempt to start the app in the Android Studio
it crashes without opening and it emulator says that unfortunately, the app has stopped. (it worked fine before without the searchview).
Main2Activity.java
package com.example.myapplication;
import androidx.annotation.NonNull;
import com.google.android.material.navigation.NavigationView;
import androidx.core.view.GravityCompat;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import androidx.core.view.MenuItemCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.content.Intent;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.content.Intent;
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
DatabaseHelper db ;
ArrayList<String> listItem;
ArrayAdapter adapter;
ListView listwine;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
db = new DatabaseHelper(this);
listItem = new ArrayList<>();
listwine = (ListView) findViewById(R.id.wine_list);
getSupportActionBar().setTitle("EOΣΣ mobile"); // for set actionbar title
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // for add back arrow in action bar
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new
Home_Fragment()).commit();
navigationView.setCheckedItem(R.id.nav_home);
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_home:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new Home_Fragment()).commit();
break;
case R.id.nav_dry_wine:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new Dry_wineFragment()).commit();
break;
case R.id.nav_semi_dry:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new
Semi_dry_wineFragment()).commit();
break;
case R.id.nav_sweet_dry:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new Sweet_wineFragment()).commit();
break;
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
MenuItem item = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) item.getActionView();
Cursor cursor = db.viewData();
if (cursor.getCount() == 0) {
Toast.makeText(this, "No data to show", Toast.LENGTH_LONG).show();
} else {
while (cursor.moveToNext()) {
listItem.add(cursor.getString(1)); //index 1 is name index 0 is id
}
}
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, listItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
// adapter.getFilter().filter(newText);
return true;
}
});
//return super.onCreateOptionsMenu(menu);
return true;
}
}
menu_search.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/item1"
android:title="Item 1"
app:showAsAction="never" />
<item
android:id="@+id/menu_search"
android:title="menuSearch"
android:icon="@drawable/ic_search"
app:showAsAction="always"
app:actionViewClass="androidx.appcompat.widget.SearchView"
/>
</menu>
DatabaseHelper.java
package com.example.myapplication;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "All_wine.db";
private static final String DB_TABLE = "all_wine_table";
private static final String DB_TABLE1 = "sweet_wine_table";
private static final String DB_TABLE2 = "semi_dry_wine_Table";
private static final String DB_TABLE3 = "dry_wine_Table";
private static final String DB_TABLE4 = "English_all_wine";
//columns
private static final String ID = "ID";
private static final String NAME ="NAME";
private static final String CREATE_TABLE ="CREATE TABLE "+DB_TABLE+" ("+
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
NAME+ " TEXT "+ ")";
private static final String CREATE_TABLE1 ="CREATE TABLE "+DB_TABLE1+" ("+
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
NAME+ " TEXT "+ ")";
private static final String CREATE_TABLE2 ="CREATE TABLE "+DB_TABLE2+" ("+
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
NAME+ " TEXT "+ ")";
private static final String CREATE_TABLE3 ="CREATE TABLE "+DB_TABLE3+" ("+
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
NAME+ " TEXT "+ ")";
private static final String CREATE_TABLE4 ="CREATE TABLE "+DB_TABLE4+" ("+
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "+
NAME+ " TEXT "+ ")";
public DatabaseHelper(Context context) {
super(context,DB_NAME ,null,1);
}
public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factoty, int version) {
super(context,DB_NAME ,null,1);
}
public DatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factoty, int version, DatabaseErrorHandler errorHandler) {
super(context,DB_NAME ,null,1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " +DB_TABLE + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT)");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Samos Grand Gru')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Vin Dux')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Samos Athemis')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Samos Nectar')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Samos Φυλλάς')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Εκκλησιαστικός Οίνος')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Aegean Breeze(semi-dry)')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Selana')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Μοσχάτο Ασπρό')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Ψηλές Κορφές')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Aegean Breeze(dry)')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Samena')");
db.execSQL("CREATE TABLE IF NOT EXISTS " +DB_TABLE1 + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT)");
db.execSQL("INSERT INTO " +DB_TABLE1 + "(NAME) VALUES ('Samos Grand Gru')");
db.execSQL("INSERT INTO " +DB_TABLE1 + "(NAME) VALUES ('Vin Dux')");
db.execSQL("INSERT INTO " +DB_TABLE1 + "(NAME) VALUES ('Samos Athemis')");
db.execSQL("INSERT INTO " +DB_TABLE1 + "(NAME) VALUES ('Samos Nectar')");
db.execSQL("INSERT INTO " +DB_TABLE1 + "(NAME) VALUES ('Samos Φυλλάς')");
db.execSQL("INSERT INTO " +DB_TABLE1 + "(NAME) VALUES ('Εκκλησιαστικός Οίνος')");
db.execSQL("CREATE TABLE IF NOT EXISTS " +DB_TABLE2 + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT)");
db.execSQL("INSERT INTO " +DB_TABLE2 + "(NAME) VALUES ('Aegean Breeze(semi-dry)')");
db.execSQL("INSERT INTO " +DB_TABLE2 + "(NAME) VALUES ('Selana')");
db.execSQL("CREATE TABLE IF NOT EXISTS " +DB_TABLE3 + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT)");
db.execSQL("INSERT INTO " +DB_TABLE3 + "(NAME) VALUES ('Μοσχάτο Ασπρό')");
db.execSQL("INSERT INTO " +DB_TABLE3 + "(NAME) VALUES ('Ψηλές Κορφές')");
db.execSQL("INSERT INTO " +DB_TABLE3 + "(NAME) VALUES ('Aegean Breeze(dry)')");
db.execSQL("INSERT INTO " +DB_TABLE3 + "(NAME) VALUES ('Samena')");
db.execSQL("CREATE TABLE IF NOT EXISTS " +DB_TABLE4 + "(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT)");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Samos Grand Gru')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Vin Dux')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Samos Athemis')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Samos Nectar')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Samos Φυλλάς')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Εκκλησιαστικός Οίνος')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Aegean Breeze(semi-dry)')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Selana')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Μοσχάτο Ασπρό')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Ψηλές Κορφές')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Aegean Breeze(dry)')");
db.execSQL("INSERT INTO " +DB_TABLE + "(NAME) VALUES ('Samena')");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i ,int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+ DB_TABLE);
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+ DB_TABLE1);
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+ DB_TABLE2);
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+ DB_TABLE3);
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "+ DB_TABLE4);
onCreate(sqLiteDatabase);
}
//create method to view data
public Cursor viewData(){
SQLiteDatabase db = this.getReadableDatabase();
String query = "Select * from " + DB_TABLE;
Cursor cursor = db.rawQuery(query,null);
return cursor;
}
//create method to view data
public Cursor viewData1(){
SQLiteDatabase db = this.getReadableDatabase();
String query = "Select * from " + DB_TABLE1;
Cursor cursor = db.rawQuery(query,null);
return cursor;
}
//create method to view data
public Cursor viewData2(){
SQLiteDatabase db = this.getReadableDatabase();
String query = "Select * from " + DB_TABLE2;
Cursor cursor = db.rawQuery(query,null);
return cursor;
}
//create method to view data
public Cursor viewData3(){
SQLiteDatabase db = this.getReadableDatabase();
String query = "Select * from " + DB_TABLE3;
Cursor cursor = db.rawQuery(query,null);
return cursor;
}
//create method to view data
public Cursor viewData4(){
SQLiteDatabase db = this.getReadableDatabase();
String query = "Select * from " + DB_TABLE4;
Cursor cursor = db.rawQuery(query, null);
return cursor;
}
public List<String> getwine() {
SQLiteDatabase db = this.getReadableDatabase();
List<String> list = new ArrayList<>();
Cursor cursor = db.rawQuery("select * from "+DB_TABLE, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
list.add(cursor.getString(0));
cursor.moveToNext();
}
cursor.close();
return list;
}
}
Error Log
08-06 14:27:43.169 497-497/? E/libsuspend: Error opening /sys/power/wakeup_count: Permission denied
08-06 14:27:43.899 497-572/? E/libsuspend: Error opening /sys/power/wakeup_count: Permission denied
08-06 14:27:44.369 497-544/? E/WifiHW: Unable to open connection to supplicant on "@android:wpa_eth1": No such file or directory
08-06 14:27:47.729 156-450/? E/BandwidthController: Updating quota globalAlert failed (No such file or directory)
08-06 14:27:59.365 679-679/? E/dalvikvm: Could not find class 'android.app.NotificationChannelGroup', referenced from method xji.a
08-06 14:28:08.565 1171-1302/? E/Finsky: [89] com.google.android.finsky.setup.ao.a(92): Error prefetching preloads, using account=null, preloads=NULL, preloadGroups=NULL
com.google.android.finsky.setup.fetchers.RawDocumentsFetchException[reason=pai_config, account=null]
at com.google.android.finsky.setup.ao.a(SourceFile:47)
at com.google.android.finsky.setup.ap.call(Unknown Source)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
08-06 14:28:09.145 800-1318/? E/MDM: [85] whz.a: No Google accounts; deferring server state update.
08-06 14:28:10.195 1371-1371/? E/UpdateRequestReceiver: ignoring update request
08-06 14:28:10.195 1371-1371/? E/UpdateRequestReceiver: ignoring update request
08-06 14:28:10.205 1371-1371/? E/UpdateRequestReceiver: ignoring update request
08-06 14:28:10.205 1371-1371/? E/UpdateRequestReceiver: ignoring update request
08-06 14:28:10.215 1371-1371/? E/UpdateRequestReceiver: ignoring update request
08-06 14:28:10.215 1371-1371/? E/UpdateRequestReceiver: ignoring update request
08-06 14:28:18.785 1171-1254/? E/dalvikvm: Could not find class 'android.app.usage.UsageStatsManager', referenced from method com.google.android.finsky.setup.a.a
08-06 14:28:21.725 156-450/? E/BandwidthController: Updating quota globalAlert failed (No such file or directory)
08-06 14:58:43.522 156-450/? E/BandwidthController: Updating quota globalAlert failed (No such file or directory)
Upvotes: 0
Views: 1081
Reputation: 1594
Check Your Project is Androidx or Android Because in Code Import is
import android.widget.SearchView;
Not Androidx(import androidx.appcompat.widget.SearchView;
) but in Xml you define
app:actionViewClass="androidx.appcompat.widget.SearchView"
Both must be Android or AndroidX Migration Doc
Upvotes: 2