android 中的片段膨胀错误

Error inflating fragment in android

在我的 android 应用程序中,我试图在我的 activity 中创建一个片段以显示来自 admobs 测试广告的广告。我正在关注本教程。 https://developers.google.com/mobile-ads-sdk/docs/admob/android/quick-start 为片段充气时出现错误。

有谁知道哪里出了问题吗?

谢谢

我的包名:android.arin.

Java

package activity;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import user.Actor;
import user.User;
import widget.Popup;
import common.FieldValidation;
import common.Transition;
import http.Network;
import android.app.Activity;
import android.app.Fragment;
import android.app.NotificationManager;
import android.arin.R;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.CompoundButton.OnCheckedChangeListener;
import arin.ArinContext;
import async.recognition.Async_recognition;
import async.user.Async_forgot_password;
import async.user.Async_login;
import async.user.Async_register;


/*
 * This activity handles the login activity.
 */

public class LoginScreen extends Activity {

    // to keep track of the views on the screen
    private EditText emailET = null;
    private EditText passwordET = null;
    private Button login = null;        
    private CheckBox checkbox = null;

    public static final String MESSAGE = "message";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_screen);

        // clear notifications
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancel(Async_recognition.SCAN_COMPLETE_NOTIFICATION_ID);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.login_screen, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        switch(id) {
            case R.id.action_forgotpassword:
                forgotPassword();
                return true;
            case R.id.action_offline_login:
                offlineLoginButtonClick();
                return true;
            case R.id.action_register:
                registerButtonClick();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }




















    @Override
    protected void onResume() {
        super.onResume();

        // checks if app started for first time
        Boolean firstTime = emailET == null;

        // reset cached data
        logout();

        // if first time, then set 
        if (firstTime) {
            recordFields();
            fillInFields();
            handleAutoLogin();
        }
    }

    private void logout() {
        ArinContext.setUser(new User(this));
    }

    private void recordFields() {
        emailET = (EditText)findViewById(R.id.email);
        passwordET = (EditText)findViewById(R.id.password);
        login = (Button) findViewById(R.id.loginButton);
        checkbox = (CheckBox) findViewById(R.id.checkBox);
    }

    private void fillInFields() {
        Actor user = ArinContext.getUser();
        String email = user.getEmail();
        String password = user.getPassword();

        Boolean autoLogin = ArinContext.getUser().isAutoLogin();
        if (email != null) emailET.setText(email);
        if (autoLogin) {
            if (password != null) passwordET.setText(password);
        } else {
            if (email == null) emailET.requestFocus();
            else passwordET.requestFocus();
        }
        checkbox.setChecked(autoLogin);
    }

    private void handleAutoLogin() {
        checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
                ArinContext.getUser().setAutoLogin(arg1);
            }
        });

        if (checkbox.isChecked() && FieldValidation.isValidEmail(emailET.getText().toString()) && FieldValidation.isValidPassword(passwordET.getText().toString())) {   
            login.performClick();
        }
    }








    public void goToNextScreen(String message) {
        if (ArinContext.saveUserData(this)) {
            Intent myIntent = new Intent(this, MenuScreen.class);
            myIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

            if (message != null) {
                myIntent.putExtra(MESSAGE, message);
            }

            startActivity(myIntent);

            Transition.TransitionForward(this);
        } else {
            Popup.ShowErrorMessage(this, R.string.localsaveerror, false);
        }
    }





    /*
     * click handlers
     */



    public void loginButtonClick(View view) {
        if (emailET != null && passwordET != null) {
            String email = emailET.getText().toString();
            String password = passwordET.getText().toString();

            Actor user = ArinContext.getUser();
            user.setEmail(email);
            user.setPassword(password);

            if (!FieldValidation.isValidEmail(email)) {
                Popup.ShowErrorMessage(this, R.string.invalid_email, false);
            } else if (!FieldValidation.isValidPassword(password)) {
                Popup.ShowErrorMessage(this, R.string.invalid_password, false);
            } else if (!Network.isNetworkAvailable(this)) {
                goToNextScreen(getString(R.string.no_internet_offline_mode));
            } else {
                new Async_login(this, email, password);
            }
        }
    }

    private void offlineLoginButtonClick() {
        goToNextScreen(getString(R.string.offline_mode));
    }

    private void forgotPassword() {
        if (Network.isNetworkAvailable(this)) {
            new Async_forgot_password(this);
        } else {
            Popup.ShowErrorMessage(this, R.string.no_internet, false);
        }
    }

    private void registerButtonClick() {
        if (Network.isNetworkAvailable(this)) {
            new Async_register(this);
        } else {
            Popup.ShowErrorMessage(this, R.string.no_internet, false);
        }
    }







    public static class AdFragment extends Fragment {

        private AdView mAdView;

        public AdFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_ad, container, false);
        }

        @Override
        public void onActivityCreated(Bundle bundle) {
            super.onActivityCreated(bundle);

            // Gets the ad view defined in layout/ad_fragment.xml with ad unit ID set in
            // values/strings.xml.
            mAdView = (AdView) getView().findViewById(R.id.adView);

            // Create an ad request. Check logcat output for the hashed device ID to
            // get test ads on a physical device. e.g.
            // "Use AdRequest.Builder.addTestDevice("ABCDEF012345") to get test ads on this device."
            AdRequest adRequest = new AdRequest.Builder()
                    .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                    .build();

            // Start loading the ad in the background.
            mAdView.loadAd(adRequest);
        }

        /** Called when leaving the activity */
        @Override
        public void onPause() {
            if (mAdView != null) {
                mAdView.pause();
            }
            super.onPause();
        }

        /** Called when returning to the activity */
        @Override
        public void onResume() {
            super.onResume();
            if (mAdView != null) {
                mAdView.resume();
            }
        }

        /** Called before the activity is destroyed */
        @Override
        public void onDestroy() {
            if (mAdView != null) {
                mAdView.destroy();
            }
            super.onDestroy();
        }
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    style="@style/RegularView"
    tools:context="activity.LoginScreen" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/arinlogo"
            android:contentDescription="@string/image_cd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/arinlogo" />

        <EditText
            android:id="@+id/email"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="10sp"
            android:layout_marginRight="10sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_email"
            android:singleLine="true"
            android:maxLength="255"
            android:ems="10"
            android:gravity="center"
            android:inputType="textEmailAddress" />

        <EditText
            android:id="@+id/password"
            android:layout_margin="10sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_password"
            android:singleLine="true"
            android:maxLength="255"
            android:ems="10"
            android:gravity="center"
            android:inputType="textPassword" />

        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/auto_login" />

        <Button
            android:id="@+id/loginButton"
            style="@style/CircleButton"
            android:onClick="loginButtonClick"
            android:text="@string/action_login" />


    </LinearLayout>

    <fragment
        android:id="@+id/adFragment"
        android:name="android.arin.activity.LoginScreen$AdFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />


</RelativeLayout>

MANIFEST(在应用程序标签内)

    <activity android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />

错误日志

01-05 00:09:05.127: E/AndroidRuntime(4978): FATAL EXCEPTION: main
01-05 00:09:05.127: E/AndroidRuntime(4978): java.lang.RuntimeException: Unable to start activity ComponentInfo{android.arin/activity.LoginScreen}: android.view.InflateException: Binary XML file line #65: Error inflating class fragment
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.app.ActivityThread.access0(ActivityThread.java:143)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.os.Looper.loop(Looper.java:137)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.app.ActivityThread.main(ActivityThread.java:4950)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at java.lang.reflect.Method.invokeNative(Native Method)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at java.lang.reflect.Method.invoke(Method.java:511)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at dalvik.system.NativeStart.main(Native Method)
01-05 00:09:05.127: E/AndroidRuntime(4978): Caused by: android.view.InflateException: Binary XML file line #65: Error inflating class fragment
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:316)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.app.Activity.setContentView(Activity.java:1915)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at activity.LoginScreen.onCreate(LoginScreen.java:52)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.app.Activity.performCreate(Activity.java:5177)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
01-05 00:09:05.127: E/AndroidRuntime(4978):     ... 11 more
01-05 00:09:05.127: E/AndroidRuntime(4978): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment android.arin.activity.LoginScreen$AdFragment: make sure class name exists, is public, and has an empty constructor that is public
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.app.Fragment.instantiate(Fragment.java:584)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.app.Fragment.instantiate(Fragment.java:552)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.app.Activity.onCreateView(Activity.java:4820)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
01-05 00:09:05.127: E/AndroidRuntime(4978):     ... 21 more
01-05 00:09:05.127: E/AndroidRuntime(4978): Caused by: java.lang.ClassNotFoundException: android.arin.activity.LoginScreen$AdFragment
01-05 00:09:05.127: E/AndroidRuntime(4978):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
01-05 00:09:05.127: E/AndroidRuntime(4978):     at android.app.Fragment.instantiate(Fragment.java:574)
01-05 00:09:05.127: E/AndroidRuntime(4978):     ... 24 more

使用

 android:name="activity.LoginScreen$AdFragment"

在 xml 中,因为 AdFragment 片段在 LoginScreen Activity 中,它在 android.arin.activity 包中,就像在 Log-Cat 中一样。