When I switch to the tab with this fragment, it always crashes. The logcat seems to say that it crashes on `mGrid = (GridView) (getView().findViewById(R.id.gridViewRed));
Any idea why?
RedScorerFragment.java
public class RedScorerFragment extends SherlockFragment {
LayoutInflater infl;
GridView mGrid;
#Override
public void onCreate(Bundle savedInstanceState){
mGrid = (GridView) (getView().findViewById(R.id.gridViewRed));
mGrid.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_MOVE)
return true;
return false;
}
});
mGrid.setAdapter(new ImageAdapter(getActivity()));
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
infl = inflater;
return inflater.inflate(R.layout.fragment_score_red, container, false);
}
fragment_score_red.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
tools:context=".RedScorerFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/gridLabel__red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:text="#string/grid_label__red"
android:background="#color/white"
/>
<GridView
android:id="#+id/gridViewRed"
android:layout_width="wrap_content"
android:layout_height="350dp"
android:layout_alignParentLeft="true"
android:layout_below="#id/gridLabel__red"
android:columnWidth="40dp"
android:numColumns="3"
android:verticalSpacing="10dp"
android:horizontalSpacing="7dp"
android:gravity="center"
android:background="#color/white"
>
</GridView>
<LinearLayout
android:id="#+id/linLayoutSide__red"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#id/gridView__red"
android:layout_alignParentLeft="true"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/weighted_label"
/>
<EditText
android:id="#+id/waitRed__red"
android:inputType="number"
android:maxLength="1"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
android:textColor="#color/red"
/>
<EditText
android:id="#+id/waitBlue__red"
android:inputType="number"
android:maxLength="1"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
android:textColor="#color/blue" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
You need to rethink where you're finding your views. This is because onCreate() is called before onCreateView()
So you should move
mGrid = (GridView) (getView().findViewById(R.id.gridViewRed));
mGrid.setOnTouchListener(new OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_MOVE)
return true;
return false;
}
});
mGrid.setAdapter(new ImageAdapter(getActivity()));
to onCreateView() This means that you need to not return the inflation, but instead work with it,
So your onCreateView() should be something like:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
infl = inflater;
View mView = inflater.inflate(R.layout.fragment_score_red, container, false);
mGrid = (GridView) (mView.findViewById(R.id.gridViewRed));
//rest of code above
return mView;
}
Related
I have used FrameLayout for showing overlay screen. While instantiating RelativeLayout, findViewById returns null.
Here is the xml file:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<me.relex.circleindicator.CircleIndicator
android:id="#+id/indicator"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginBottom="48dp"
android:layout_gravity="bottom"
app:ci_drawable="#drawable/orange_radius"
app:ci_drawable_unselected="#drawable/white_radius"/>
</RelativeLayout>
<!--Below is the transparent layout positioned at startup -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#70000000"
android:id="#+id/topLayout">
<ImageView
android:id="#+id/ivInstruction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="25dp"
android:layout_marginRight="15dp"
android:clickable="false"
android:paddingLeft="20dip"
android:scaleType="center"
android:src="#drawable/home" />
</RelativeLayout>
Fragment code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = getActivity().getLayoutInflater().inflate(R.layout.our_work_layout, null);
topLayout = (RelativeLayout) mView.findViewById(R.id.topLayout);
if (isFirstTime()) {
topLayout.setVisibility(View.INVISIBLE);
}
ViewPager viewpager = (ViewPager) mView.findViewById(R.id.viewpager);
// mPager.setAdapter(mAdapter);
ImageAdapter adapter = new ImageAdapter(act);
viewpager.setAdapter(adapter);
CircleIndicator indicator = (CircleIndicator) mView.findViewById(R.id.indicator);;
indicator.setViewPager(viewpager);
return mView;
}
private boolean isFirstTime()
{
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
boolean ranBefore = preferences.getBoolean("RanBefore", false);
if (!ranBefore) {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", true);
editor.commit();
topLayout.setVisibility(View.VISIBLE);
topLayout.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
topLayout.setVisibility(View.INVISIBLE);
return false;
}
});
}
return ranBefore;
}
Your problem is here:
ViewPager viewpager = (ViewPager) mView.findViewById(R.id.viewpager);
There is no "viewpager" in this layout. You used the id "pager".
Correct this line to the following:
ViewPager viewpager = (ViewPager) mView.findViewById(R.id.pager);
The title of this question is vague because I don't really know what is happening. I hava list view inside a class called SummerJobFragment.java this list view has a onItemClicked() that is suppose to open and other fragment called SummerJobDetailsFragment.java. Below I've posted the code and the logcat screenshot.
SummerJobFragment.java
public class SummerJobsFragmnet extends Fragment {
public SummerJobsFragmnet() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static Fragment getInstance() {
Fragment fragment = new SummerJobsFragmnet();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void showMessage (String title, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final MainActivity activity = (MainActivity) getActivity();
String [] places = activity.getAllPositionsNamesPhone().toArray(
new String[activity.getAllPositionsNamesPhone().size()]);
final ListView list = (ListView) getView().findViewById(R.id.joblistView);
int prgmImages=R.mipmap.ic_launcher;
list.setAdapter(new CustomListAdapter(activity,places,prgmImages));
// OnClick listner for the individual cells of the listView
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
SummerJobDetailsFragment.mMyAppsBundle.putInt("value", position);
SummerJobDetailsFragment fragment = new SummerJobDetailsFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.layout.displayjobs_detailed, fragment).commit();
}
});
}
SummerJobDetailsFragment.java
public class SummerJobDetailsFragment extends Fragment {
DataBaseHelper summerJobDB;
public static Bundle mMyAppsBundle = new Bundle();
public int position = SummerJobDetailsFragment.mMyAppsBundle.getInt("value");
public SummerJobDetailsFragment() {
// Required empty public constructor
}
public static Fragment getInstance() {
Fragment fragment = new SummerJobsFragmnet();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.displayjobs_detailed,container,false);
super.onViewCreated(view, savedInstanceState);
SummerJobDetailsFragment summer = new SummerJobDetailsFragment();
DataBaseHelper summerJobDB;
summerJobDB = new DataBaseHelper(getActivity());
Cursor res = summerJobDB.getAllData(position+1);
EditText jobPlace = (EditText)view.findViewById(R.id.jobTitle);
jobPlace.setText(res.getString(1));
/*
EditText jobPlace = (EditText)summer.getView().findViewById(R.id.jobTitle);
jobPlace.setText(res.getString(1));
EditText jobPosition = (EditText)summer.getView().findViewById(R.id.jobPlace);
jobPlace.setText(res.getString(2));
EditText starTime = (EditText)summer.getView().findViewById(R.id.jobStartingTime);
jobPlace.setText(res.getString(3));
EditText address = (EditText)summer.getView().findViewById(R.id.jobAddress);
jobPlace.setText(res.getString(5));
EditText phone = (EditText)summer.getView().findViewById(R.id.jobPhoneNum);
jobPlace.setText(res.getString(6));
EditText hours = (EditText)summer.getView().findViewById(R.id.jobHours);
jobPlace.setText(res.getString(4));
*/
return
}
}
and here is the displayjobs_detail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/LinearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/jobplace"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobPlace"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Job Title"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobTitle"
android:layout_gravity="right" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Job Address:"
android:id="#+id/jobAddress" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobAddress" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Phone number:"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobPhoneNum"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hours"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobHours" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Starting Time"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobStartingTime" />
</LinearLayout>
and here is the logcat:
First of all you are doing the layout operations in onCreate, you should do it in onCreateView() like this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_songs, container, false);
EditText jobPlace = (EditText)rootView.findViewById(R.id.jobTitle);
jobPlace.setText(res.getString(1));
...
...
}
Also remove this statement
SummerJobDetailsFragment summer = new SummerJobDetailsFragment();
it doesn't make any sense, you call findViewById on the rootView.
No, this is how you do it,
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/displayjobs_detailed"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/LinearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/jobplace"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobPlace"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Job Title"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobTitle"
android:layout_gravity="right" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Job Address:"
android:id="#+id/jobAddress" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobAddress" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Phone number:"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobPhoneNum"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Hours"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobHours" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Starting Time"
android:id="#+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/jobStartingTime" />
</LinearLayout>
</FrameLayout>
Then in your fragment use this to commit
fragmentManager.beginTransaction().replace(R.id.displayjobs_detailed, fragment).commit(); // Note R.id instead of R.layout
Initialize your view items in onCreateView of the Fragment not in onCreate
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_layout, container, false);
// Initialize Here
return view;
}
You are calling wrong. Difference between onCreateView and onViewCreated in Fragment
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
Instead of, call
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.your_layout, container, false);
// Initialize Here
return view;
}
This is my fragment class:
public class FragmentDdayMonthly extends Fragment {
ListView lvFrequency;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dday_monthly, container, false);
ArrayList<MonthlyModel> monthlyModels = new ArrayList<MonthlyModel>();
monthlyModels.add(null);
lvFrequency = (ListView) view.findViewById(R.id.list_frequency);
DDayMonthlyListAdapter adapter = new DDayMonthlyListAdapter(getActivity(),monthlyModels,lvFrequency);
lvFrequency.setAdapter(adapter);
return view;
}
This is adapter:
public class DDayMonthlyListAdapter extends BaseAdapter implements View.OnClickListener {
private Context mContext;
private ArrayList<MonthlyModel> monthlyModels;
ListView listView;
public DDayMonthlyListAdapter(Context context,ArrayList<MonthlyModel> monthlyModels, ListView listView) {
this.mContext = context;
this.monthlyModels = monthlyModels;
this.listView = listView;
}
public int getCount() {
return monthlyModels.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// inflate the GridView item layout
LayoutInflater inflater = LayoutInflater.from(mContext);
convertView = inflater.inflate(R.layout.item_monthly_list, parent, false);
return convertView;
}
Layout fragment_dday_monthly just have a Listview. And here is layout of Listview's item (R.layout.item_monthly_list):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/frequency_choose_container_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dday_activity_divider"
android:layout_alignBottom="#+id/frequency_choose_container_1">
<Spinner
android:id="#+id/spin_chosen_month"
android:layout_width="#dimen/spinner_frequency_width"
android:layout_height="wrap_content"
android:background="#android:color/transparent"/>
<ImageView
android:id="#+id/img_chosen_month_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/spin_chosen_month"
android:src="#drawable/add_ic_arrow" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/frequency_choose_container_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/dday_activity_divider">
<TextView
android:id="#+id/txt_chosen_month"
android:layout_width="#dimen/spinner_frequency_width"
android:layout_height="wrap_content"
android:text="Monthly"
android:textSize="#dimen/sp14_text_size" />
<ImageView
android:id="#+id/img_chosen_month_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/txt_chosen_month"
android:src="#drawable/add_ic_arrow" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/frequency_monthly_picker_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/frequency_choose_container_2"
android:layout_marginTop="10dp"
>
<RadioButton
android:id="#+id/rb_frequency_date_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/radiobutton_selector" />
<RelativeLayout
android:id="#+id/date_choose_container_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/rb_frequency_date_5"
android:background="#drawable/dday_activity_divider">
<Spinner
android:id="#+id/spin_chosen_date_2"
android:layout_width="#dimen/spinner_chosen_date_width"
android:layout_height="wrap_content"
android:text="15"
android:textSize="#dimen/sp14_text_size"
android:background="#android:color/transparent"/>
<ImageView
android:id="#+id/img_chosen_date_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/spin_chosen_date_2"
android:src="#drawable/add_ic_arrow" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/date_choose_container_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/rb_frequency_date_5"
android:background="#drawable/dday_activity_divider">
<TextView
android:id="#+id/txt_chosen_date_1"
android:layout_width="#dimen/spinner_chosen_date_width"
android:layout_height="wrap_content"
android:text="15"
android:textSize="#dimen/sp14_text_size" />
<ImageView
android:id="#+id/img_chosen_date_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/txt_chosen_date_1"
android:src="#drawable/add_ic_arrow" />
</RelativeLayout>
<RadioButton
android:id="#+id/rb_frequency_week_n_month_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/date_choose_container_2"
android:button="#drawable/radiobutton_selector" />
<RelativeLayout
android:id="#+id/daily_choose_container_child_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/rb_frequency_week_n_month_5"
android:background="#drawable/dday_activity_divider">
<TextView
android:id="#+id/txt_chosen_day_child_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weekly"
android:textSize="#dimen/sp14_text_size" />
<ImageView
android:id="#+id/img_chosen_day_child_6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/txt_chosen_day_child_6"
android:src="#drawable/add_ic_arrow" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/daily_choose_container_child_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/daily_choose_container_child_6"
android:background="#drawable/dday_activity_divider">
<TextView
android:id="#+id/txt_chosen_day_child_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Monday"
android:textSize="#dimen/sp14_text_size" />
<ImageView
android:id="#+id/img_chosen_day_child_7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#+id/txt_chosen_day_child_7"
android:src="#drawable/add_ic_arrow" />
</RelativeLayout>
<ImageView
android:id="#+id/img_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/margin_right_10dp"
android:src="#drawable/add_ic_delete" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/img_delete"
android:layout_alignLeft="#+id/img_delete"
android:layout_toLeftOf="#+id/img_add"
android:layout_marginTop="10dp"
android:src="#drawable/add_ic_add" />
</RelativeLayout>
</RelativeLayout>
As you can see I just inflate view to fragment. I dont do any heavy task, but the fragment load so long, it take 1-2 second to be shown. Please help me to fix it!
Instead of inflating , setting adapter in onCreateView do those things in onViewCreated() method
public class FragmentDdayMonthly extends Fragment {
ListView lvFrequency;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dday_monthly, container, false);
ArrayList<MonthlyModel> monthlyModels = new ArrayList<MonthlyModel>();
monthlyModels.add(null);
lvFrequency = (ListView) view.findViewById(R.id.list_frequency);
DDayMonthlyListAdapter adapter = new DDayMonthlyListAdapter(getActivity(),monthlyModels,lvFrequency);
lvFrequency.setAdapter(adapter);
return view;
}
}
Like this
public class FragmentDdayMonthly extends Fragment {
ListView lvFrequency;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dday_monthly, container, false);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
ArrayList<MonthlyModel> monthlyModels = new ArrayList<MonthlyModel>();
monthlyModels.add(null);
lvFrequency = (ListView) view.findViewById(R.id.list_frequency);
DDayMonthlyListAdapter adapter = new DDayMonthlyListAdapter(getActivity(),monthlyModels,lvFrequency);
lvFrequency.setAdapter(adapter);
}
}
There is no need of passing Arraylist To DDayMonthlyListAdapter as this is not necessary in your case
also you should modify the code as below:
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return position;
}
This must work as the solution!>..
I have custom dialog to show which has instruction about how to use the app.I am using ViewPager for this inside my custom dialog layout.
I am getting error
java.lang.IllegalArgumentException: No view found for id for fragment FragmentForInstruction1.
I have created a method which is called in onCreate(Bundle savedInstanceState) {} method .The method inflates the layout for dialog.
private void showCustomDialogForInstruction() {
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogLayout = inflater.inflate(R.layout.dialog_layout_for_instruction_message, null, false);
layout.setAlpha(0.2f);
dialog.setCanceledOnTouchOutside(false);
dialog.setContentView(dialogLayout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
ViewPager viewPager=(ViewPager) dialog.findViewById(R.id.pagerInstruction);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new FragmentForInstruction1());
adapter.addFrag(new Fragme`enter code here`ntForWelcomePage2());
adapter.addFrag(new FragmentForWelcomePage3());
adapter.addFrag(new FragmentForWelcomePage4());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new OnPageChangeListener()
{
#Override
public void onPageSelected(int pos)
{
}
#Override
public void onPageScrolled(int pos, float arg1, int arg2)
{
}
#Override
public void onPageScrollStateChanged(int arg0)
{
}
});
Button done = (Button) dialog.findViewById(R.id.done);
done.setText("Got It");
done.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
Button neverShow = (Button) dialog.findViewById(R.id.nevershow);
neverShow.setText("Never Show Again");
neverShow.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
});
dialog.getWindow().setBackgroundDrawable(
new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.show();
}
my view pager class:
public class ViewPagerAdapter extends FragmentPagerAdapter
{
private final List<Fragment> mFragmentList = new ArrayList<Fragment>();
public ViewPagerAdapter(FragmentManager manager)
{
super(manager);
}
#Override
public Fragment getItem(int position)
{
return mFragmentList.get(position);
}
#Override
public int getCount()
{
return mFragmentList.size();
}
public void addFrag(Fragment fragment)
{
mFragmentList.add(fragment);
}
}
and i call the method in MainActivity :
public class MainActivity extends AppCompatActivity {
SharedPreferences sharedPreferencesNeverShowAgain ;
boolean neverShowAgain;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sharedPreferencesNeverShowAgain = PreferenceManager.getDefaultSharedPreferences(this);
neverShowAgain = sharedPreferencesNeverShowAgain.getBoolean("NeverShowAgain", false);
if(!neverShowAgain){
showCustomDialogForMessage();
}
}
}
and the layout for dialog is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/dialog_shape"
android:orientation="vertical" >
<!-- layout title -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#00000000"
android:gravity="center"
android:orientation="vertical" >
<ViewFlipper
android:id="#+id/flipper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/top_edge_rounded"
android:flipInterval="2000"
android:padding="20dp" >
<TextView
android:id="#+id/dialog_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:text="Input Height"
android:textColor="#color/textColor"
android:textStyle="normal" />
<TextView
android:id="#+id/title1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center"
android:text="Input Height"
android:textColor="#color/accent"
android:textStyle="normal" />
</ViewFlipper>
<View
android:id="#+id/tri"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="#drawable/triangle"
android:rotation="180" />
</LinearLayout>
<!-- layout dialog content -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="300dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<android.support.v4.view.ViewPager
android:id="#+id/pagerInstruction"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="#+id/layoutIndicater"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>
</ScrollView>
<!-- layout dialog buttons -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_margin="10dp"
android:background="#drawable/all_rounded_edge_plum_for_dialog" >
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_alignParentTop="true"
android:background="#color/textColor" />
<View
android:id="#+id/ViewColorPickerHelper"
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#color/textColor" />
<Button
android:id="#+id/nevershow"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/ViewColorPickerHelper"
android:background="?android:attr/selectableItemBackground"
android:padding="5dp"
android:text="Close"
android:textColor="#color/textColor" />
<Button
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ViewColorPickerHelper"
android:background="?android:attr/selectableItemBackground"
android:padding="5dp"
android:text="Done"
android:textColor="#color/textColor" />
</RelativeLayout>
</LinearLayout>
FragmentForInstruction1 code:
public class FragmentForInstruction1 extends Fragment{
#Override
#Nullable
public View onCreateView(LayoutInflater inflater,
#Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragmentinstructionpage1, container,false);
return view;
}
}
and its layout is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/backgroundColor"
android:gravity="center" >
<TextView
android:id="#+id/titleWelcomeFragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/colorPrimary"
android:textSize="30sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/messageWelcomeFragment1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:textStyle="normal"
android:paddingLeft="20dp"
android:paddingRight="20dp"/>
</LinearLayout>
You should create an adapter that extends FragmentPagerAdapter:
private class CustomPagerAdapter extends FragmentPagerAdapter {
public CustomPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new FragmentForInstruction1();
case 1:
return new FragmentForInstruction2();
case 2:
return new FragmentForInstruction3();
case 3:
return new FragmentForInstruction4();
default:
return new FragmentForInstruction1();
}
}
#Override
public int getCount() {
return 4;
}
}
While crating an instance of it, pass the getChildFragmentManager() instead of getSupportFragmentManager():
viewPager.setAdapter(new CustomPagerAdapter(getChildFragmentManager()));
I'm completing part of the Google Android Tutorial (this part to be precise) and I've hit a snag. I'm trying to set the Adapter of a ListView to a custom ArrayAdapter. Unfortunately, findViewById seems to be returning null (which means I can't set the adapter).
Here's the relevant code:
MainActivityFragment
private final String LOG_TAG = MainActivityFragment.class.getSimpleName();
private ArtistItemAdapter artistsAdapter;
public MainActivityFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
artistsAdapter = new ArtistItemAdapter(getActivity(), R.layout.list_item_artist, new Pager<Artist>().items);
ListView artistsList = (ListView) rootView.findViewById(R.id.listViewArtists);
artistsList.setAdapter(artistsAdapter);
return rootView;
}
#Override
public void onStart() {
EditText searchBar = (EditText) getView().findViewById(R.id.searchEditText);
searchBar.setOnEditorActionListener(new TextView.OnEditorActionListener() {
ArtistsPager artists;
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEARCH) {
FetchArtistsTask artistsTaskCall = new FetchArtistsTask();
artistsTaskCall.execute(String.valueOf(v.getText()));
handled = true;
}
return handled;
}
});
}
MainActivity.onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
fragment_main.xml
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivityFragment">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/searchEditText"
android:inputType="textFilter"
android:imeOptions="actionSearch"
android:drawableLeft="#android:drawable/ic_menu_search"
android:hint="Search Artist"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/listViewArtists"
android:layout_below="#+id/searchEditText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
activity_main.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="#+id/fragment"
android:name="com.example.android.spotifyproject.MainActivityFragment"
tools:layout="#layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
LogCat: http://pastebin.com/CUNF05Af
It's not the ListView that is null, it's the data you sent into the Adapter when you created it, so when you try to set the Adapter on the ListView it calls List.size() on the data(null), and that's where the error is.