调用片段时出错

时间:2015-09-12 05:06:44

标签: android android-layout android-fragments android-intent android-activity

大家好我已经创建了一个片段类并包含在我的项目中。现在问题是我所包含的类片段不起作用而且它不幸地停止了。它显示这样的错误。

09-12 10:26:31.066    7892-7892/com.sentientit.theiWedplanner E/AndroidRuntime? FATAL EXCEPTION: main
Process: com.sentientit.theiWedplanner, PID: 7892
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sentientit.theiWedplanner/com.sentientit.theiWedplanner.Frontpage}: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2411)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
        at android.os.Handler.dispatchMessage(Handler.java:110)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5323)
        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:825)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
        at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:341)
        at android.app.Activity.setContentView(Activity.java:1948)
        at com.sentientit.theiWedplanner.Frontpage.onCreate(Frontpage.java:41)
        at android.app.Activity.performCreate(Activity.java:5343)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2323)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2411)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5323)
            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:825)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
            at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.sentientit.theiWedplanner.Fragadmob: make sure class name exists, is public, and has an empty constructor that is public
        at android.app.Fragment.instantiate(Fragment.java:597)
        at android.app.Fragment.instantiate(Fragment.java:561)
        at android.app.Activity.onCreateView(Activity.java:4841)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:341)
            at android.app.Activity.setContentView(Activity.java:1948)
            at com.sentientit.theiWedplanner.Frontpage.onCreate(Frontpage.java:41)
            at android.app.Activity.performCreate(Activity.java:5343)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2323)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2411)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5323)
            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:825)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
            at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.sentientit.theiWedplanner.Fragadmob" on path: DexPathList[[zip file "/system/framework/com.google.android.maps.jar", zip file "/data/app/com.sentientit.theiWedplanner-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.sentientit.theiWedplanner-1, /vendor/lib, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
        at android.app.Fragment.instantiate(Fragment.java:583)
            at android.app.Fragment.instantiate(Fragment.java:561)
            at android.app.Activity.onCreateView(Activity.java:4841)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:341)
            at android.app.Activity.setContentView(Activity.java:1948)
            at com.sentientit.theiWedplanner.Frontpage.onCreate(Frontpage.java:41)
            at android.app.Activity.performCreate(Activity.java:5343)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2323)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2411)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
            at android.os.Handler.dispatchMessage(Handler.java:110)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:5323)
            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:825)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)
            at dalvik.system.NativeStart.main(Native Method)

片段xml文件

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
     android:id="@+id/adview153613"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:background="@drawable/aagr_background2" >
           <ImageView        
              android:id="@+id/addviewimage"
              android:layout_width="fill_parent"
              android:layout_height="50dp"                    
              android:src="@drawable/adview1536132" />        

      <com.google.android.gms.ads.AdView
       xmlns:ads="http://schemas.android.com/apk/res-auto"
       android:id="@+id/adView"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       ads:adSize="SMART_BANNER"
       ads:adUnitId="-------------"
         />
       <ImageView
          android:id="@+id/addviewimage1"
          android:layout_width="@dimen/fwh"
          android:layout_height="@dimen/fwh"
          android:layout_alignParentRight="true"
          android:layout_marginRight="23dp"
          android:src="@drawable/close" />

 </RelativeLayout>

片段java文件

package com.sentientit.theiWedplanner;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class Fragadmob extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragadmob, container,   false);

        //Admob        
         RelativeLayout gonad=(RelativeLayout)rootView.findViewById(R.id.adview153613);
         ImageView addviewimage=(ImageView)rootView.findViewById(R.id.addviewimage);
         ImageView inap=(ImageView)rootView.findViewById(R.id.addviewimage1);

         //   AdView adView = new AdView(getActivity());
           AdView adView = (AdView)rootView.findViewById(R.id.adView);
          AdRequest adRequest = new AdRequest.Builder()
              .build();
          adView.loadAd(adRequest);
         addviewimage.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                 Intent viewIntent =
                          new Intent("android.intent.action.VIEW",
                            Uri.parse("https://play.google.com/store/apps/developer?id=SentientIT+America,+LLC"));
                          startActivity(viewIntent);                

            }

         });
         inap.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                     Intent intent = new Intent(getActivity(),Inapp.class);
                     startActivity(intent);         

                }

             });

          if(addviewimage.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) 
            {            
             addviewimage.getLayoutParams().height = 30;                    
                // code to do for Portrait Mode
            } 
    return rootView;
    }


}

gradle文件

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.sentientit.theiWedplanner"
        minSdkVersion 9
        targetSdkVersion 21
        versionCode 13
        versionName "11.0"
        multiDexEnabled true
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/LICENSE.txt'

    }
    lintOptions {
        checkReleaseBuilds false
        // Or, if you prefer, you can continue to check for errors in release builds,
        // but continue the build even when errors are found:
        abortOnError false
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
        debug {
            debuggable true
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'

        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:22.2.0'
    compile 'joda-time:joda-time:2.4'
    compile files('libs/bugsense-3.6.1.jar')
    compile files('libs/core.jar')
    compile files('libs/MtUtils.jar')
    compile files('libs/socialauth-android-3.2.jar')
    compile files('libs/universal-image-loader-1.6.1-with-src.jar')
    compile files('libs/zxingjar-1.1.jar')
    compile 'com.google.android.gms:play-services:7.5.0'
    compile files('libs/ksoap2.jar')
    compile 'com.android.support:multidex:1.0.1'
    compile files('libs/twitter4j.jar')

}

请给我解决好友的可能解决方案

提前谢谢

验证xml

时出现

错误

Error:(8, 55) cvc-elt.1: Cannot find the declaration of element 'RelativeLayout'.
Error:(1, 56) s4s-elt-schema-ns: The namespace of element 'x' must be from the schema namespace, 'http://www.w3.org/2001/XMLSchema'.
Error:(1, 56) s4s-elt-invalid: Element 'x' is not a valid element in a schema document.
Error:(1, 56) schema_reference.4: Failed to read schema document 'null', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

类加载片段文件

package com.sentientit.theiWedplanner;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;

import com.bugsense.trace.BugSenseHandler;
import com.google.android.gms.ads.AdView;

public class Frontpage extends FragmentActivity {
    /** Called when the activity is first created. */
    SharedPreferences sharedPreferences;

    @Override
      public void onStart() {
        super.onStart();
       // The rest of your onStart() code.

        // //  EasyTracker.getInstance(this).activityStart(this);  // Add this method.

      }

      @Override
      public void onStop() {
        super.onStop();
        // The rest of your onStop() code.

        // EasyTracker.getInstance(this).activityStop(this);  // Add this method.
      }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);       BugSenseHandler.initAndStartSession(this, "68640bea");
        //requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

        setContentView(R.layout.front);
       // getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.main1);
        //TextView ctitle=(TextView)findViewById(R.id.customtitlebar);
        //ctitle.setText("Resources");

        AdView adview=(AdView)findViewById(R.id.ad);
        if(GlobalClass.adm){
         adview.setVisibility(View.GONE);
        }


      RelativeLayout clic =(RelativeLayout)findViewById(R.id.fron);
       clic.setOnClickListener(new OnClickListener() {

            //@Override
            @Override
            public void onClick(View v) 
            {
                // TODO Auto-generated method stub              
                Intent intent=new Intent(Frontpage.this,welcomeuser1.class);
                //Intent intent=new Intent(Frontpage.this,ZXingJarDemoActivity.class);
                startActivity(intent);
                finish();
            } 



    });



    }

    }

1 个答案:

答案 0 :(得分:0)

注意此日志消息

Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class fragment

您在使用xml视图文件时遇到问题。最有可能排在第11行。你可以尝试删除

xmlns:ads="http://schemas.android.com/apk/res-auto" 
来自adview的

。名称空间在根元素中声明。此外,fill_parent被折旧。使用match_parent