I have a ViewPager that holds three fragments (within a tabbed layout). The third and final fragment holds two ImageButtons (up and down arrows) and an ImageView that cycles through different images depending on which directional arrow is hit. So far, I've been unable to get the onClick methods on the ImageButtons to work. I understand that the code first looks for the onClick method in the parent Activity's source, if I used the onClick in the Fragment's XML. So I tried defining an onClick listener based on existing solutions to similar questions on StackOverflow, but this approach doesn't seem to be working. Can some please help me understand if I'm doing something wrong or if there's another mistake altogether?
Here's the Fragment's XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#2E2E2E" >
<ImageButton
android:id="@+id/pageUpButton"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:src="@drawable/uparrow"
android:layout_marginTop="5dp"
android:contentDescription="@string/contentDesc"
android:background="@drawable/arrow_imagebutton_selector" />
<ImageView
android:id="@+id/recipeDirectionsImage"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:contentDescription="@string/contentDesc" />
<ImageButton
android:id="@+id/pageDownButton"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center"
android:src="@drawable/downarrow"
android:contentDescription="@string/contentDesc"
android:background="@drawable/arrow_imagebutton_selector" />
<!-- make list recipePagesList -->
</LinearLayout>
Fragment's java code: Note: I've only shown the use of one of the buttons in this code Note: pageUp and pageDown are the previous and next pages respectively.
public class RecipeDirectionsFragment extends Fragment //implements OnClickListener
{
int REQUEST_CODE = 0, RESULT_OK = 1, page_num = 1;
float x1,x2;
float y1, y2;
public ImageView recipePage;
// private ImageButton pageButton = null;
private ImageButton upPageButton = null;
private ImageButton downPageButton = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_recipe_directions, container, false);
recipePage = (ImageView) rootView.findViewById(R.id.recipeDirectionsImage);
changePage();
upPageButton = (ImageButton)rootView.findViewById(R.id.upButton);
downPageButton = (ImageButton)rootView.findViewById(R.id.downButton);
// pageButton = (ImageButton)rootView.findViewById(R.id.upButton);
// pageButton.setOnClickListener(this);
ImageButton upPageButton = (ImageButton) rootView.findViewById(R.id.upButton);
upPageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pageUp(v);
}
});
ImageButton downPageButton = (ImageButton) rootView.findViewById(R.id.downButton);
downPageButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
pageDown(v);
}
});
return rootView;
}
public void changePage()
{
//recipePage = (ImageView) getView().findViewById(R.id.recipeDirectionsImage);
switch(page_num)
{
case 1: recipePage.setImageResource(R.drawable.recipe_1_3); break;
case 2: recipePage.setImageResource(R.drawable.recipe_1_4); break;
case 3: recipePage.setImageResource(R.drawable.recipe_1_5); break;
case 4: recipePage.setImageResource(R.drawable.recipe_1_6); break;
case 5: recipePage.setImageResource(R.drawable.recipe_1_7); break;
case 6: recipePage.setImageResource(R.drawable.recipe_1_8); break;
}
}
public void pageUp(View view)
{
//Toast.makeText(getActivity(), "up", Toast.LENGTH_SHORT).show();
page_num = (page_num > 1) ? page_num-1 : 1;
changePage();
}
public void pageDown(View view)
{
//Toast.makeText(getActivity(), "down", Toast.LENGTH_SHORT).show();
page_num = (page_num < 6) ? page_num+1 : 6;
changePage();
}
}
With the above code, every time I switch to this fragment, the app just crashes. It doesn't even show me load the fragment.
I've even tried using this alternate overridden onClick function and making the class implement onClickListener :
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.upButton: Toast.makeText(getActivity(), "up", Toast.LENGTH_SHORT).show();
break;
case R.id.downButton: Toast.makeText(getActivity(), "down", Toast.LENGTH_SHORT).show();
break;
}
}
This too doesn't work and crashes the app.
Another solution I came across recommended using the paeUp and pageDown functions in the activity instead but I need to use the variable page_num and don't know how to access it in the Activity using some sort of a FragmentManager. Also, I would rather keep the functions in the Fragment and follow the approaches shown earlier in the question.
Any help would be most appreciated! NOTE: I'm working with API 21 and don't know if that makes any difference.
Edit: Logcat output snippet at the moment the fragment fails to load (gives a dialogue "Unfortunately has stopped" and rolls back to the previous activity)
03-12 19:51:24.793: D/EN(23743): [ReminderService] - ReminderService: total time to run = 43 millis
03-12 19:51:24.886: D/AndroidRuntime(23698): Shutting down VM
03-12 19:51:24.887: E/AndroidRuntime(23698): FATAL EXCEPTION: main
03-12 19:51:24.887: E/AndroidRuntime(23698): Process: com.example.culami, PID: 23698
03-12 19:51:24.887: E/AndroidRuntime(23698): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
03-12 19:51:24.887: E/AndroidRuntime(23698): at com.example.culami.RecipeDirectionsFragment.onCreateView(RecipeDirectionsFragment.java:39)
03-12 19:51:24.887: E/AndroidRuntime(23698): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
03-12 19:51:24.887: E/AndroidRuntime(23698): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
03-12 19:51:24.887: E/AndroidRuntime(23698): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
03-12 19:51:24.887: E/AndroidRuntime(23698): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
03-12 19:51:24.887: E/AndroidRuntime(23698): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
03-12 19:51:24.887: E/AndroidRuntime(23698): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
03-12 19:51:24.887: E/AndroidRuntime(23698): at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
03-12 19:51:24.887: E/AndroidRuntime(23698): at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
03-12 19:51:24.887: E/AndroidRuntime(23698): at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
03-12 19:51:24.887: E/AndroidRuntime(23698): at android.support.v4.view.ViewPager$3.run(ViewPager.java:249)
03-12 19:51:24.887: E/AndroidRuntime(23698): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
03-12 19:51:24.887: E/AndroidRuntime(23698): at android.view.Choreographer.doCallbacks(Choreographer.java:580)
03-12 19:51:24.887: E/AndroidRuntime(23698): at android.view.Choreographer.doFrame(Choreographer.java:549)
03-12 19:51:24.887: E/AndroidRuntime(23698): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
03-12 19:51:24.887: E/AndroidRuntime(23698): at android.os.Handler.handleCallback(Handler.java:739)
03-12 19:51:24.887: E/AndroidRuntime(23698): at android.os.Handler.dispatchMessage(Handler.java:95)
03-12 19:51:24.887: E/AndroidRuntime(23698): at android.os.Looper.loop(Looper.java:135)
03-12 19:51:24.887: E/AndroidRuntime(23698): at android.app.ActivityThread.main(ActivityThread.java:5221)
03-12 19:51:24.887: E/AndroidRuntime(23698): at java.lang.reflect.Method.invoke(Native Method)
03-12 19:51:24.887: E/AndroidRuntime(23698): at java.lang.reflect.Method.invoke(Method.java:372)
03-12 19:51:24.887: E/AndroidRuntime(23698): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
03-12 19:51:24.887: E/AndroidRuntime(23698): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
03-12 19:51:24.888: W/ActivityManager(546): Force finishing activity com.example.culami/.ActiveRecipeActivit
y
Aucun commentaire:
Enregistrer un commentaire