我正在创建一个带有搜索工具的片段列表。我试图将ActionBarSherlock中的代码移植到appcompat。完成了大部分所需的更改。我收到空指针异常错误。
java.lang.NullPointerException
at com.example.rand.LatestFragment.onCreateOptionsMenu(LatestFragment.java:266)
at android.support.v4.app.Fragment.performCreateOptionsMenu(Fragment.java:1582)
at android.support.v4.app.FragmentManagerImpl.dispatchCreateOptionsMenu(FragmentManager.java:1967)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:225)
at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:232)
at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:146)
at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:199)
at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:293)
at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:436)
at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:800)
at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:221)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:543)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
LatestFragment.java
public class LatestFragment extends Fragment {
ListView lsv_latest;
List<ItemLatest> arrayOfLatestMusic;
LatestGridAdapter objAdapter;
AlertDialogManager alert = new AlertDialogManager();
ArrayList<String> allListImageUrl,allListMusicCatName,allListMusicShare;
ArrayList<String> allListMusicId,allListMusicCatId,allListMusicUrl,allListMusicName,allListMusicDuration,allListMusicDesc;
String[] allArrayImage,allArrayMusicCatName,allArrayMusicShare;
String[] allArrayMusicId,allArrayMusicCatId,allArrayMusicurl,allArrayMusicName,allArrayMusicDuration,allArrayMusicDesc;
private ItemLatest objAllBean;
private int columnWidth;
JsonUtils util;
int textlength = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.fragment_latest, container, false);
setHasOptionsMenu(true);
lsv_latest=(ListView)rootView.findViewById(R.id.lsv_latest);
arrayOfLatestMusic=new ArrayList<ItemLatest>();
allListImageUrl=new ArrayList<String>();
allListMusicCatName=new ArrayList<String>();
allListMusicCatId=new ArrayList<String>();
allListMusicId=new ArrayList<String>();
allListMusicName=new ArrayList<String>();
allListMusicUrl=new ArrayList<String>();
allListMusicDuration=new ArrayList<String>();
allListMusicDesc=new ArrayList<String>();
allListMusicShare=new ArrayList<String>();
allArrayImage=new String[allListImageUrl.size()];
allArrayMusicCatName=new String[allListMusicCatName.size()];
allArrayMusicId=new String[allListMusicId.size()];
allArrayMusicCatId=new String[allListMusicCatId.size()];
allArrayMusicurl=new String[allListMusicUrl.size()];
allArrayMusicName=new String[allListMusicName.size()];
allArrayMusicDuration=new String[allListMusicDuration.size()];
allArrayMusicDesc=new String[allListMusicDesc.size()];
allArrayMusicShare=new String[allListMusicShare.size()];
util=new JsonUtils(getActivity());
if (JsonUtils.isNetworkAvailable(getActivity())) {
new MyTask().execute(Constant.LATEST_URL);
} else {
showToast("No Network Connection!!!");
alert.showAlertDialog(getActivity(), "Internet Connection Error",
"Please connect to working Internet connection", false);
}
lsv_latest.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
objAllBean=arrayOfLatestMusic.get(position);
int pos=objAllBean.getId();
Constant.MUSIC_ID=String.valueOf(pos);
Constant.MUSIC_PLAY_ID="";
Log.e("Music_id", Constant.MUSIC_ID);
Intent intplay=new Intent(getActivity(),SongPlay1.class);
intplay.putExtra("POSITION", pos);
intplay.putExtra("MP3_IMAGE", allArrayImage);
intplay.putExtra("MP3_CATNAME", allArrayMusicCatName);
intplay.putExtra("MP3_CATID", allArrayMusicCatId);
intplay.putExtra("MP3_URL", allArrayMusicurl);
intplay.putExtra("MP3_NAME", allArrayMusicName);
intplay.putExtra("MP3_CID", allArrayMusicId);
intplay.putExtra("MP3_DURATION", allArrayMusicDuration);
intplay.putExtra("MP3_DISCRIPTION", allArrayMusicDesc);
intplay.putExtra("MP3_SHARE", allArrayMusicShare);
startActivity(intplay);
}
});
return rootView;
}
private class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
return JsonUtils.getJSONString(params[0]);
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if (null != pDialog && pDialog.isShowing()) {
pDialog.dismiss();
}
if (null == result || result.length() == 0) {
showToast("Server Connection Error");
alert.showAlertDialog(getActivity(), "Server Connection Error",
"May Server Under Maintainance Or Low Network", false);
} else {
try {
JSONObject mainJson = new JSONObject(result);
JSONArray jsonArray = mainJson.getJSONArray(Constant.LATEST_ARRAY_NAME);
JSONObject objJson = null;
for (int i = 0; i < jsonArray.length(); i++) {
objJson = jsonArray.getJSONObject(i);
ItemLatest objItem = new ItemLatest();
objItem.setId(objJson.getInt(Constant.LATEST_ID));
objItem.setCategoryId(objJson.getInt(Constant.LATEST_CATID));
objItem.setCategoryName(objJson.getString(Constant.LATEST_CAT_NAME));
objItem.setMp3Name(objJson.getString(Constant.LATEST_MP3_NAME));
objItem.setMp3Url(objJson.getString(Constant.LATEST_MP3_URL));
objItem.setShareUrl(objJson.getString(Constant.LATEST_MP3_SHARE_URL));
objItem.setDescription(objJson.getString(Constant.LATEST_MP3_DESCRIPTION));
objItem.setDuration(objJson.getString(Constant.LATEST_MP3_DURATION));
objItem.setImage(objJson.getString(Constant.LATEST_MP3_IMAGE_THUMB));
arrayOfLatestMusic.add(objItem);
}
} catch (JSONException e) {
e.printStackTrace();
}
for(int j=0;j<arrayOfLatestMusic.size();j++)
{
objAllBean=arrayOfLatestMusic.get(j);
allListImageUrl.add(objAllBean.getImage());
allArrayImage=allListImageUrl.toArray(allArrayImage);
allListMusicCatName.add(objAllBean.getCategoryName());
allArrayMusicCatName=allListMusicCatName.toArray(allArrayMusicCatName);
allListMusicId.add(String.valueOf(objAllBean.getId()));
allArrayMusicId=allListMusicId.toArray(allArrayMusicId);
allListMusicCatId.add(String.valueOf(objAllBean.getCategoryId()));
allArrayMusicCatId=allListMusicCatId.toArray(allArrayMusicCatId);
allListMusicUrl.add(String.valueOf(objAllBean.getMp3Url()));
allArrayMusicurl=allListMusicUrl.toArray(allArrayMusicurl);
allListMusicName.add(String.valueOf(objAllBean.getMp3Name()));
allArrayMusicName=allListMusicName.toArray(allArrayMusicName);
allListMusicDuration.add(String.valueOf(objAllBean.getDuration()));
allArrayMusicDuration=allListMusicDuration.toArray(allArrayMusicDuration);
allListMusicDesc.add(objAllBean.getDescription());
allArrayMusicDesc=allListMusicDesc.toArray(allArrayMusicDesc);
allListMusicShare.add(objAllBean.getShareUrl());
allArrayMusicShare=allListMusicShare.toArray(allArrayMusicShare);
}
setAdapterToListview();
}
}
}
public void setAdapterToListview() {
objAdapter = new LatestGridAdapter(getActivity(), R.layout.latest_lsv_item,
arrayOfLatestMusic,columnWidth);
lsv_latest.setAdapter(objAdapter);
}
public void showToast(String msg) {
Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
}
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_search, menu);
final MenuItem searchMenuItem = menu.findItem(R.id.search);
//MenuItem searchItem = menu.findItem(R.id.search);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
// final SearchView searchView = (SearchView) MenuItemCompat.getActionView(R.id.search);
// final SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
// return super.onCreateOptionsMenu(menu);
searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if (!hasFocus) {
searchMenuItem.collapseActionView();
searchView.setQuery(" ", false);
}
}
});
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(String newText) {
textlength = newText.length();
arrayOfLatestMusic.clear();
for (int i = 0; i < allArrayMusicName.length; i++) {
if (textlength <= allArrayMusicName[i].length()) {
if (newText.toString().equalsIgnoreCase((String) allArrayMusicName[i].subSequence(0, textlength))) {
ItemLatest objItem = new ItemLatest();
objItem.setId(Integer.parseInt(allArrayMusicId[i]));
objItem.setCategoryId(Integer.parseInt(allArrayMusicCatId[i]));
objItem.setCategoryName(allArrayMusicCatName[i]);
objItem.setDescription(allArrayMusicDesc[i]);
objItem.setDuration(allArrayMusicDuration[i]);
objItem.setImage(allArrayImage[i]);
objItem.setMp3Name(allArrayMusicName[i]);
objItem.setMp3Url(allArrayMusicurl[i]);
objItem.setShareUrl(allArrayMusicShare[i]);
arrayOfLatestMusic.add(objItem);
}
}
}
setAdapterToListview();
return false;
}
@Override
public boolean onQueryTextSubmit(String query) {
// Do something
return true;
}
});
}
}
menu_search.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/search"
android:title="@string/menu_search"
android:icon="@drawable/search"
app:showAsAction="collapseActionView|always"
android:actionViewClass="android.support.v7.widget.SearchView"/>
</menu>
fragment_latest.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f8f8f8"
android:id="@+id/rootlayout"
android:padding="5dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="8dp"
android:background="@drawable/rounded_corner">
<ListView
android:id="@+id/lsv_latest"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000"
android:divider="@color/divider"
android:dividerHeight="2dp"
android:padding="5dp"
android:listSelector="@drawable/list_selector">
</ListView>
</LinearLayout>