我正在尝试按标题,名称,日期和标记对我的Recyclerview进行排序,但它正在给我
以下错误: -
06-04 08:55:24.560 18488-18488/com.droidslife.sarkarijobs D/AndroidRuntime: Shutting down VM
06-04 08:55:24.560 18488-18488/com.droidslife.sarkarijobs W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x5cd4ab20)
06-04 08:55:24.560 18488-18488/com.droidslife.sarkarijobs I/Process: Sending signal. PID: 18488 SIG: 9
06-04 08:55:24.560 18488-18488/com.droidslife.sarkarijobs D/AndroidRuntime: procName from cmdline: com.droidslife.sarkarijobs
06-04 08:55:24.560 18488-18488/com.droidslife.sarkarijobs E/AndroidRuntime: in writeCrashedAppName, pkgName :com.droidslife.sarkarijobs
06-04 08:55:24.560 18488-18488/com.droidslife.sarkarijobs D/AndroidRuntime: file written successfully with content: com.droidslife.sarkarijobs StringBuffer : ;com.droidslife.sarkarijobs
06-04 08:55:24.560 18488-18488/com.droidslife.sarkarijobs E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.droidslife.sarkarijobs, PID: 18488
java.lang.NullPointerException
at com.droidslife.sarkarijobs.Formdir.FormMainFragment.onClick(FormMainFragment.java:142)
at android.view.View.performClick(View.java:4499)
at android.view.View$PerformClick.run(View.java:18571)
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:5021)
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:827)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
at dalvik.system.NativeStart.main(Native Method)
06-04 08:56:35.620 19212-19212/com.droidslife.sarkarijobs I/dalvikvm: Could not find method android.content.Context.getSystemService, referenced from method com.droidslife.sarkarijobs.app.MyApplication.access$super
06-04 08:56:35.620 19212-19212/com.droidslife.sarkarijobs W/dalvikvm: VFY: unable to resolve virtual method 586: Landroid/content/Context;.getSystemService (Ljava/lang/Class;)Ljava/lang/Object;
06-04 08:56:35.630 19212-19212/com.droidslife.sarkarijobs D/dalvikvm: VFY: replacing opcode 0x6f at 0x0035
06-04 08:56:35.630 19212-19212/com.droidslife.sarkarijobs I/dalvikvm: Could not find method android.content.Context.getColorStateList, referenced from method com.droidslife.sarkarijobs.app.MyApplication.access$super
这是我的主要片段: -
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.droidslife.sarkarijobs.R;
import com.droidslife.sarkarijobs.extras.SortListener;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionButton;
import com.oguzdev.circularfloatingactionmenu.library.FloatingActionMenu;
import com.oguzdev.circularfloatingactionmenu.library.SubActionButton;
import java.util.ArrayList;
import java.util.List;
public class FormMainFragment extends Fragment implements View.OnClickListener {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_form_main_items = 3;
//tag associated with the FAB menu button that sorts by name
private static final String TAG_SORT_NAME = "sortName";
//tag associated with the FAB menu button that sorts by date
private static final String TAG_SORT_DATE = "sortDate";
//tag associated with the FAB menu button that sorts by ratings
private static final String TAG_SORT_RATINGS = "sortRatings";
private FloatingActionButton mFAB;
private FloatingActionMenu mFABMenu;
private MyFormMainAdapter myFormMainAdapter;
public FormMainFragment() {
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
*Inflate tab_layout and setup Views.
*/
View formMainView = inflater.inflate(R.layout.form_main_layout, null);
tabLayout = (TabLayout) formMainView.findViewById(R.id.tabs);
viewPager = (ViewPager) formMainView.findViewById(R.id.viewpager);
/**
*Set an Apater for the View Pager
*/
viewPager.setAdapter(new MyFormMainAdapter(getChildFragmentManager()));
/**
* Now , this is a workaround ,
* The setupWithViewPager dose't works without the runnable .
* Maybe a Support Library Bug .
*/
tabLayout.post(new Runnable() {
@Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
//define the icon for the main floating action button
ImageView iconFAB = new ImageView(getActivity());
iconFAB.setImageResource(R.drawable.ic_action_new);
//set the appropriate background for the main floating action button along with its icon
mFAB = new FloatingActionButton.Builder(getActivity())
.setContentView(iconFAB)
.setBackgroundDrawable(R.drawable.selector_button_red)
.build();
//define the icons for the sub action buttons
ImageView iconSortName = new ImageView(this.getActivity());
iconSortName.setImageResource(R.drawable.ic_action_alphabets);
ImageView iconSortDate = new ImageView(this.getActivity());
iconSortDate.setImageResource(R.drawable.ic_action_calendar);
ImageView iconSortRatings = new ImageView(this.getActivity());
iconSortRatings.setImageResource(R.drawable.ic_action_important);
//set the background for all the sub buttons
SubActionButton.Builder itemBuilder = new SubActionButton.Builder(getActivity());
itemBuilder.setBackgroundDrawable(getResources().getDrawable(R.drawable.selector_sub_button_gray));
//build the sub buttons
SubActionButton buttonSortName = itemBuilder.setContentView(iconSortName).build();
SubActionButton buttonSortDate = itemBuilder.setContentView(iconSortDate).build();
SubActionButton buttonSortRatings = itemBuilder.setContentView(iconSortRatings).build();
//to determine which button was clicked, set Tags on each button
buttonSortName.setTag(TAG_SORT_NAME);
buttonSortDate.setTag(TAG_SORT_DATE);
buttonSortRatings.setTag(TAG_SORT_RATINGS);
buttonSortName.setOnClickListener(this);
buttonSortDate.setOnClickListener(this);
buttonSortRatings.setOnClickListener(this);
//add the sub buttons to the main floating action button
mFABMenu = new FloatingActionMenu.Builder(getActivity())
.addSubActionView(buttonSortName)
.addSubActionView(buttonSortDate)
.addSubActionView(buttonSortRatings)
.attachTo(mFAB)
.build();
return formMainView;
}
@Override
public void onClick(View v) {
//call instantiate item since getItem may return null depending on whether the PagerAdapter is of type FragmentPagerAdapter or FragmentStatePagerAdapter
Fragment fragment = (Fragment) myFormMainAdapter.instantiateItem(viewPager, viewPager.getCurrentItem());
if (fragment instanceof SortListener) {
if (v.getTag().equals(TAG_SORT_NAME)) {
//call the sort by name method on any Fragment that implements sortlistener
((SortListener) fragment).onSortByName();
}
if (v.getTag().equals(TAG_SORT_DATE)) {
//call the sort by date method on any Fragment that implements sortlistener
((SortListener) fragment).onSortByDate();
}
if (v.getTag().equals(TAG_SORT_RATINGS)) {
//call the sort by ratings method on any Fragment that implements sortlistener
((SortListener) fragment).onSortByRating();
}
}
}
class MyFormMainAdapter extends FragmentPagerAdapter {
public MyFormMainAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FormFragment();
case 1:
return new AdmitFragment();
case 2:
return new ResultFragment();
}
return null;
}
@Override
public int getCount() {
return int_form_main_items;
}
/**
* This method returns the title of the tab according to the position.
*/
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "New Jobs";
case 1:
return "Admit Cards";
case 2:
return "Results";
}
return null;
}
}
}
这是表单片段: -
public class FormFragment extends Fragment implements FormsLoadedListener,SwipeRefreshLayout.OnRefreshListener,SortListener,SearchView.OnQueryTextListener {
//The key used to store arraylist of movie objects to and from parcelable
private static final String STATE_FORM = "state_forms";
//the arraylist containing our list of box office his
private ArrayList<FormItem> mListForms = new ArrayList<>();
//the adapter responsible for displaying our movies within a RecyclerView
private AdapterForms mAdapter;
private SwipeRefreshLayout mSwipeRefreshLayout;
//the recyclerview containing showing all our movies
private RecyclerView mRecyclerForms;
//the TextView containing error messages generated by Volley
private TextView mTextError;
private FormSorter mSorter = new FormSorter() ;
//the sorter responsible for sorting our movie results based on choice made by the user in the FAB
// private MovieSorter mSorter = new MovieSorter();
public FormFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment FragmentBoxOffice.
*/
// TODO: Rename and change types and number of parameters
public static FormFragment newInstance(String param1, String param2) {
FormFragment fragment = new FormFragment();
Bundle args = new Bundle();
//put any extra arguments that you may want to supply to this fragment
fragment.setArguments(args);
return fragment;
}
public void onFormsLoaded() {
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.form_layout,container,false);
mTextError = (TextView) layout.findViewById(R.id.textVolleyError);
mSwipeRefreshLayout = (SwipeRefreshLayout) layout.findViewById(R.id.swipeFormHits);
mSwipeRefreshLayout.setOnRefreshListener(this);
mRecyclerForms =(RecyclerView) layout.findViewById(R.id.listFormHits);
//set the layout manager before trying to display data
mRecyclerForms.setLayoutManager(new LinearLayoutManager(getActivity()));
mAdapter = new AdapterForms(getActivity());
mRecyclerForms.setAdapter(mAdapter);
if (savedInstanceState != null){
mListForms = savedInstanceState.getParcelableArrayList(STATE_FORM);
} else {
mListForms = MyApplication.getWritableDatabase().readForms(DBForms.FORMS);
if (mListForms.isEmpty()){
L.m("FormFragment: executing task from fragment");
new TaskLoadForms(this).execute();
}
}
mAdapter.setForms(mListForms);
return layout;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelableArrayList(STATE_FORM, mListForms);
}
private void handleVolleyError(VolleyError error) {
//if any error occurs in the network operations, show the TextView that contains the error message
mTextError.setVisibility(View.VISIBLE);
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
mTextError.setText(R.string.error_timeout);
} else if (error instanceof AuthFailureError) {
mTextError.setText(R.string.error_auth_failure);
//TODO
} else if (error instanceof ServerError) {
mTextError.setText(R.string.error_auth_failure);
//TODO
} else if (error instanceof NetworkError) {
mTextError.setText(R.string.error_network);
//TODO
} else if (error instanceof ParseError) {
mTextError.setText(R.string.error_parser);
//TODO
}
}
/** * Called when the user chooses to sort results by name through the menu displayed inside FAB
**/
@Override
public void onSortByName() {
L.t(getActivity(),"sorted by nme");
// mSorter.sortFormsByName(mListForms);
// mAdapter.notifyDataSetChanged();
}
/**
* Called when the user chooses to sort results by date through the menu displayed inside FAB
**/
@Override
public void onSortByDate() {
// mSorter.sortFormsByDate(mListForms);
// mAdapter.notifyDataSetChanged();
}
@Override
public void onSortByRating() {
}
/**
* Called when the user chooses to sort results by rating through the menu displayed inside FAB
@Override
public void onSortByRating() {
mSorter.sortMoviesByRating(mListMovies);
mAdapter.notifyDataSetChanged();
}
/**
* Called when the AsyncTask finishes load the list of movies from the web
**/
@Override
public void onFormsLoaded(ArrayList<FormItem> listForms) {
L.m("FormFRagment: Onformloaded fragment");
if (mSwipeRefreshLayout.isRefreshing()){
mSwipeRefreshLayout.setRefreshing(false);
}
mAdapter.setForms(listForms);
}
@Override
public void onRefresh() {
L.m("onReFresh");
new TaskLoadForms(this).execute();
我还想添加一个searchView过滤器,以便我可以按标题搜索recyclerView数据,如果有人有任何教程请注释链接 }
}
答案 0 :(得分:0)
尝试初始化适配器。它目前为空。
myFormMainAdapter = new MyFormMainAdapter(getChildFragmentManager());
viewPager.setAdapter(myFormMainAdapter);