rupesh
rupesh

Reputation: 2891

View pager is not working

here i am implementing view pager but it is showing me some error.I have debugged my code i am able to get all the data don't know where i am missing. here i am pasting my code with LogCat please try to have a look in this:

LogCat:

12-31 18:14:41.201: E/AndroidRuntime(13699): FATAL EXCEPTION: main
12-31 18:14:41.201: E/AndroidRuntime(13699): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lenovo.bsharp/com.lenovo.bsharp.ProductDetailsActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.app.ActivityThread.access$700(ActivityThread.java:143)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.os.Looper.loop(Looper.java:137)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.app.ActivityThread.main(ActivityThread.java:4960)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at java.lang.reflect.Method.invokeNative(Native Method)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at java.lang.reflect.Method.invoke(Method.java:511)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at dalvik.system.NativeStart.main(Native Method)
12-31 18:14:41.201: E/AndroidRuntime(13699): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:318)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.app.Activity.setContentView(Activity.java:1925)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at com.lenovo.bsharp.ProductDetailsActivity.onCreate(ProductDetailsActivity.java:64)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.app.Activity.performCreate(Activity.java:5203)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
12-31 18:14:41.201: E/AndroidRuntime(13699):    ... 11 more
12-31 18:14:41.201: E/AndroidRuntime(13699): Caused by: java.lang.ClassCastException: com.lenovo.bsharp.ProductDetailsTopFragment cannot be cast to android.support.v4.app.Fragment
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.support.v4.app.Fragment.instantiate(Fragment.java:394)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
12-31 18:14:41.201: E/AndroidRuntime(13699):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682)

Activity Class:

public class ProductDetailsActivity extends FragmentActivity {
private static String cookie;
private static String nid;
private static String productName;
private static byte[] imgByteArray;
private static int productRating;
ViewPager viewPager = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent intent = getIntent();
    cookie = intent.getStringExtra(BsharpConstant.WEB_SERVICES_COOKIES);
    nid = intent.getStringExtra(BsharpConstant.PRODUCT_NODE_ID);
    productName = intent.getStringExtra(BsharpConstant.PRODUCT_NAME);
    productRating = intent.getIntExtra(BsharpConstant.RATE_VALUE, 1);
    imgByteArray = intent.getByteArrayExtra(BsharpConstant.IMAGE_BITMAP);

    // Top fragments
    Bundle bundleForProductDetailsInTopFrag = new Bundle();
    bundleForProductDetailsInTopFrag.putString(BsharpConstant.PRODUCT_NAME,
            productName);
    bundleForProductDetailsInTopFrag.putByteArray(
            BsharpConstant.IMAGE_BITMAP, imgByteArray);
    bundleForProductDetailsInTopFrag.putInt(BsharpConstant.RATE_VALUE,
            productRating);
    ProductDetailsTopFragment.newInstance(bundleForProductDetailsInTopFrag);

    // LeftFragment
    Bundle bundleForProductDetailsInLeftFrag = new Bundle();
    bundleForProductDetailsInLeftFrag.putString(
            BsharpConstant.WEB_SERVICES_COOKIES, cookie);
    bundleForProductDetailsInLeftFrag.putString(
            BsharpConstant.PRODUCT_NODE_ID, nid);
    bundleForProductDetailsInLeftFrag.putString(
            BsharpConstant.PRODUCT_NAME, productName);
    ProductDetailsLeftFragment
            .newInstance(bundleForProductDetailsInLeftFrag);
    // Right Fragment
    Bundle bundleForProductDetailsInRightFrag = new Bundle();
    bundleForProductDetailsInRightFrag.putString(
            BsharpConstant.PRODUCT_NODE_ID, nid);
    bundleForProductDetailsInRightFrag.putString(
            BsharpConstant.WEB_SERVICES_COOKIES, cookie);
    ProductDetailsRightFragment
            .newInstance(bundleForProductDetailsInRightFrag);
    setContentView(R.layout.product_details);
    viewPager = (ViewPager) findViewById(R.id.pager);
    FragmentManager fragmentManager = getSupportFragmentManager();
    viewPager.setAdapter(new MyAdapter(fragmentManager));

}

public class MyAdapter extends FragmentPagerAdapter {

    public MyAdapter(android.support.v4.app.FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }

    @Override
    public Fragment getItem(int pageNo) {
        Fragment fragment = null;
        if (pageNo == 0) {
            fragment = new ProductDetailsLeftFragment();
        }
        if (pageNo == 1) {
            fragment = new ProductDetailsRightFragment();
        }
        if (pageNo == 2) {
            fragment = new ProductDetailsPagerThirdFragment();
        }

        return fragment;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return 3;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        if (position == 0) {
            return "Description";
        } else if (position == 1) {
            return "Tech Spec";
        } else if (position == 2) {
            return "Features";
        }
        return null;
    }

}
}

Fragment A:

public class ProductDetailsLeftFragment extends Fragment{
private static String cookie;
private static String nid;
private static String prodName;
TextView prodNameId;
private static String nodeJsonResponseString;
ProductDetailsInterface productDetailsInterface;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View fragProDetailsLeftView = inflater.inflate(
            R.layout.product_details_left_fragment, container, false);
    prodNameId = (TextView) fragProDetailsLeftView
            .findViewById(R.id.getFirstFragment);
    return fragProDetailsLeftView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    productDetailsInterface = (ProductDetailsInterface) getActivity();
    nodeJsonResponseString = WebserviceBsharpUtil
            .callWebServicesGetSpecificProductsDetails(cookie, nid);
    prodNameId.setText(prodName);
    // productImageId.setImageBitmap(pro)
}

static ProductDetailsLeftFragment newInstance(Bundle bundle) {
    ProductDetailsLeftFragment productDetailsLeftFragment = new ProductDetailsLeftFragment();
    productDetailsLeftFragment.setArguments(bundle);
    cookie = bundle.getString(BsharpConstant.WEB_SERVICES_COOKIES);
    nid = bundle.getString(BsharpConstant.PRODUCT_NODE_ID);
    prodName = bundle.getString(BsharpConstant.PRODUCT_NAME);
    return productDetailsLeftFragment;
}

 }

Fragment B:

public class ProductDetailsRightFragment extends Fragment {
private static String cookie;
private static String nid;
TextView getProductDetailsDescription;
private static String nodeJsonResponseString;
private static String properDescriptionResponse;
private static String properTechSpecResponse;
private static int id;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View fragProDetailsRightView = inflater.inflate(
            R.layout.product_details_right_fragment, container, false);
    getProductDetailsDescription = (TextView) fragProDetailsRightView
            .findViewById(R.id.getSecondFragment);
    return fragProDetailsRightView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    nodeJsonResponseString = WebserviceBsharpUtil
            .callWebServicesGetSpecificProductsDetails(cookie, nid);
    String getDescription = ProductCategoryIds
            .parseJSONResponseToGetVidOfProductsFromVocabulary(
                    nodeJsonResponseString, BsharpConstant.DESC_BODY,
                    BsharpConstant.UND, BsharpConstant.SAFE_VALUE);
    properDescriptionResponse = getDescription.replaceAll("\\<.*?>", "");
    getProductDetailsDescription
            .setText(properDescriptionResponse);

}

static ProductDetailsRightFragment newInstance(Bundle bundle) {
    ProductDetailsRightFragment productDetailsRightFragment = new ProductDetailsRightFragment();
    productDetailsRightFragment.setArguments(bundle);
    cookie = bundle.getString(BsharpConstant.WEB_SERVICES_COOKIES);
    nid = bundle.getString(BsharpConstant.PRODUCT_NODE_ID);
    return productDetailsRightFragment;
}
 }

Fragment C:

  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.TextView;

  public class ProductDetailsPagerThirdFragment extends Fragment {
TextView featuresText;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View fragProDetailsThirdView = inflater.inflate(
            R.layout.product_details_right_fragment, container, false);
    featuresText = (TextView) fragProDetailsThirdView
            .findViewById(R.id.getThirdFragment);
    return fragProDetailsThirdView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
      featuresText.setText("Fragment C");
  }
  }

This is my activity XML file:

fragmentTopFragment i want to be freez so i am not using that fragment in view Pager

<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:orientation="vertical"
tools:context=".ProductDetails" >

<fragment
    android:id="@+id/fragmentTopFragment"
    android:name="com.lenovo.bsharp.ProductDetailsTopFragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <android.support.v4.view.PagerTitleStrip
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:id = "@+id/title"
            android:layout_gravity="bottom"
            android:paddingTop="4dp"
            android:paddingBottom="4dp">
        </android.support.v4.view.PagerTitleStrip>
    </android.support.v4.view.ViewPager>
   </RelativeLayout>

 </LinearLayout>

Thanks for your support.

Updated Logcat:

12-31 18:54:06.481: E/AndroidRuntime(16605): FATAL EXCEPTION: main
12-31 18:54:06.481: E/AndroidRuntime(16605): java.lang.NullPointerException
12-31 18:54:06.481: E/AndroidRuntime(16605):    at com.lenovo.bsharp.ProductDetailsPagerThirdFragment.onActivityCreated(ProductDetailsPagerThirdFragment.java:26)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1468)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at android.support.v4.view.ViewPager$3.run(ViewPager.java:237)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at android.view.Choreographer.doFrame(Choreographer.java:524)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at android.os.Handler.handleCallback(Handler.java:615)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at android.os.Handler.dispatchMessage(Handler.java:92)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at android.os.Looper.loop(Looper.java:137)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at android.app.ActivityThread.main(ActivityThread.java:4960)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at java.lang.reflect.Method.invokeNative(Native Method)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at java.lang.reflect.Method.invoke(Method.java:511)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
12-31 18:54:06.481: E/AndroidRuntime(16605):    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 248

Answers (2)

Renaud Mathieu
Renaud Mathieu

Reputation: 376

Your imports are probably wrong in ProducDetailsTopFragment. Make sure you are using android.support.v4.app.Fragment

Upvotes: 0

Henrique
Henrique

Reputation: 5011

You're problem seems to be here:

12-31 18:14:41.201: E/AndroidRuntime(13699): Caused by: java.lang.ClassCastException: com.lenovo.bsharp.ProductDetailsTopFragment cannot be cast to android.support.v4.app.Fragment

Make sure your ProductDetailsTopFragment class is extending android.support.v4.app.Fragment and not Fragment.

Or you can post the entire code for ProductDetailsTopFragment so we can see if that's the issue or not.

Upvotes: 2

Related Questions