这里我正在实现查看寻呼机,但它显示了一些错误。我调试了我的代码我能够得到所有数据不知道我在哪里缺少。在这里我用LogCat粘贴我的代码请试着看看:
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)
活动类:
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;
}
}
}
片段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;
}
}
片段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;
}
}
片段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");
}
}
这是我的活动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: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>
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)
答案 0 :(得分:2)
你的问题似乎在这里:
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
确保您的ProductDetailsTopFragment
课程正在延长android.support.v4.app.Fragment
而不是Fragment
。
或者您可以发布ProductDetailsTopFragment
的完整代码,以便我们查看是否存在问题。
答案 1 :(得分:0)
您的导入在ProducDetailsTopFragment中可能有误。 确保您使用的是android.support.v4.app.Fragment