Android widget SearchView in onCreateOptionsMenu is null
If your class extends AppCompatActivity
Check if linked appropriate library for:
Check if you have to properly sett SearchView in menu xml for onCreateOptionsMenu menu_main.xml
Check your SearchableActivity onCreateOptionsMenu
public class SearchableActivity extends AppCompatActivity
Check if linked appropriate library for:
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
Check if you have to properly sett SearchView in menu xml for onCreateOptionsMenu menu_main.xml
<menu xmlns:android="//schemas.android.com/apk/res/android" xmlns:app="//schemas.android.com/apk/res-auto"
xmlns:tools="//schemas.android.com/tools"
tools:context=".SearchableActivity">
<item
android:id="@+id/menu_search_my"
android:title="@string/menu_search"
android:icon="@drawable/ic_menu_search"
app:showAsAction="collapseActionView|ifRoom"
app:actionViewClass="android.support.v7.widget.SearchView"
/></menu>
Check your SearchableActivity onCreateOptionsMenu
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @SuppressLint("NewApi") @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
android.support.v7.widget.SearchView searchView = (android.support.v7.widget.SearchView)
MenuItemCompat.getActionView(menu.findItem(R.id.menu_search_my));
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
if(null!=searchManager ) {
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
}
searchView.setIconifiedByDefault(false);
}
return true;
}
396LW NO topic_id
AD
Další témata ....(Topics)
// file name MyFirstClass.java
import java.util.*;
import java.lang.Math;
import java.io.*;
import javax.swing.*;
public class MyFirstClass{ // start of program
public static void main(String[] args) { // basic function main
// variables and calculation
int a=2;
int b=3;
int c=Integer.parseInt(JOptionPane.showInputDialog(" Put number: ", "1"));
System.out.println("Number is: "+c);
System.out.println(a+" * "+b+" = "+(a*b));
System.out.println("a^3 "+Math.pow(a,b));
//array
int[] array_my=new int[10];
array_my[0]=3;
array_my[1]=5;
System.out.println("Number of elements "+array_my.length+" 1 + 2 element of array "+(array_my[0]+array_my[1]));
//strings
String txt="Quick red fox";
String txt2=JOptionPane.showInputDialog("Write text: ", "word");
System.out.println("Text is: "+txt2);
String[] count_of_word=txt.split(" ");
System.out.println("Length: "+txt.length()+" Count of words "+count_of_word.length);
System.out.println(txt +" -> "+txt.replace("red","brown"));
System.out.println("First 5 chars of string is: "+ txt.substring(0,5));
//for a if
for(int i=0; i<10;i++){
if(i==3)System.out.println("i equal "+i);
}
// file
try {
File soubor=new File("some_file.txt");
if(!soubor.exists()){
System.out.println("File dont exist");
}
else { // utf-8 encoding
BufferedReader in1 = new BufferedReader(new InputStreamReader(new FileInputStream(soubor),"UTF-8"));
BufferedWriter out1 = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("zapis.txt"),"UTF-8"));
String str;
while ((str = in1.readLine())!=null){
System.out.println(str);
out1.write(str+"
");
}
in1.close();
out1.close();
}
} catch(IOException e){System.out.println("Error " + e);}
// dir
File pathName = new File("some_dir");
String[] fileNames = pathName.list();
if(pathName.exists())
System.out.println("Name of first file in "some_dir": "+fileNames[0]);
else System.out.println("dir not exist");
//function
int nResult = calculateMyFc(3,5);
System.out.println("Result of function: "+nResult);
} // end of function main
// new function , you can add to end MyFc = my function
public static int calculateMyFc(int a, int b){
return (a+b);
}
} // end of class of program
If you using Context as parameter of function try this solution:
public class MyActivity extends Activity {
// bla bla bla .......
//error
myFc( getapplicationcontext());
// OK
myFc(MyActivity.this);
Map TreeMap key value pair, Map sort by key, Iterator for Map Java Android example.
MainClass.java
MainClass.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
public class MainClass {
public static void main(String[] arg) {
// english;germany dictionary
String[] arrayOfString = { "one;eine", "two;zwei", "three;drei", "four;vier" };
Map<String, String> map = new TreeMap<String, String>();
for(String s: arrayOfString){
String[] array = s.split(";");
String sKey ="", sValue="";
if(array.length > 1){
sKey = array[0]; sValue = array[1];
map.put(sKey, sValue);
}
}
List sortedByKeys=new ArrayList(map.keySet());
Collections.sort(sortedByKeys);
// iterate map
Set references = map.keySet();
Iterator it = references.iterator();
while (it.hasNext()) {
String key = (String) it.next();
String value = map.get(key);
System.out.println(key + " = " + value);
}
// or other example how iterate map
TreeSet<String> keys = new TreeSet<String>(map.keySet());
for (String key : keys) {
String value = map.get(key);
System.out.println(key + " = " + value);
}
// check if key exists
// if( map.containsKey("two")){
// System.out.print("two = " + map.get("two"));
// }
}
}
/*
four = vier
one = eine
three = drei
two = zwei
*/
HashMap<String,Locale> _mapOfLocale = new HashMap<String,Locale>();
_mapOfLocale.put("French",Locale. FRENCH );
_mapOfLocale.put("German",Locale. GERMAN );
_mapOfLocale.put("Italian",Locale. ITALIAN );
for (Entry<String, Locale> entry : _mapOfLocale.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
Editace: 2015-11-25 09:49:36
Počet článků v kategorii: 396
Url:android-widget-searchview-in-oncreateoptionsmenu-is-null