错误夸大类开关ActionBarSherlock(InflateException) [英] Error inflating class switch, ActionBarSherlock (InflateException)

查看:144
本文介绍了错误夸大类开关ActionBarSherlock(InflateException)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个错误的痛苦。我有一个SherlockFragmentActivity,至少有两个SherlockFragments。两者都使用onCreateView来膨胀xml文件。但是,LoginFragment有一个开关小部件:econ片段没有。如果我在econfragment.xml中添加一个开关小部件,则会导致相同类型的崩溃(而loginfragment被注释为不在实现中)。如果我从loginfragment中删除了开关小部件,RadioGroup会显示类似的错误-但是电子配给已具有一个无线电组,并且不会产生任何错误。以下是相关代码:

I'm running into a pain of an error. I have a SherlockFragmentActivity, and at least two SherlockFragments. Both use onCreateView to inflate an xml file. LoginFragment, however, has a switch widget: econ fragment does not. If I add a switch widget into econfragment.xml, that causes a crash of the same type (while loginfragment is commented out of implementation). If I remove the switch widget from loginfragment, a similar error shows up for RadioGroup - but econfragment already has a radio group and produces no errors. Here is the relevant code:

轮询:

import java.util.ArrayList;
import library.DatabaseHandler;
import org.json.JSONObject;
import com.actionbarsherlock.R;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;

public class Polling extends SherlockFragmentActivity {
    private ViewPager mViewPager;
    private TabsAdapter mTabsAdapter;
    private final static String TAG = "21st Polling:";
    private Button loginButton;
    private Button registerButton;
    private Switch rememberSwitch;
    SharedPreferences sharedPreferences;
    Toast toast;

    public void loginReport(int responseCode) {
        //if login succeeded
        if (responseCode == 1) {
            loginButton = (Button)findViewById(R.id.loginButton);
            loginButton.setText("Log Out");
            Context context = getApplicationContext();
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(context, "Logged in.", duration);
            toast.show();

        }
        if (responseCode == 0) {
            Context context = getApplicationContext();
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(context, "Incorrect username/password", duration);
            toast.show();
        }
        if (responseCode == 2) {
            //remove user from active sql db here rather than LoginFragment?!
            Context context = getApplicationContext();
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, "Logged out", duration);
            toast.show();
        }
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v(TAG, "onCreate");
        mViewPager = new ViewPager(this);
        mViewPager.setId(R.id.pager);
        setContentView(mViewPager);
        ActionBar bar = getSupportActionBar();
        bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        bar.setDisplayShowTitleEnabled(false);
        bar.setDisplayShowHomeEnabled(false);

        mTabsAdapter = new TabsAdapter(this, mViewPager);
        //mTabsAdapter.addTab(bar.newTab().setText(R.string.login),
                //LoginFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.economics),
                EconFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.elections),
                ElectionsFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.politics),
                PoliticsFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.science),
                ScienceFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.finance),
                FinanceFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.religion),
                ReligionFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.military),
                MilitaryFragment.class, null);
        mTabsAdapter.addTab(bar.newTab().setText(R.string.international),
                InternationalFragment.class, null); 
        //Log.v(TAG, (String)bar.getTabAt(0).getText());

    }

    public void login(int id)
    {
        JSONObject json_userJsonObject;
        //DatabaseHandler db = new DatabaseHandler(getApplicationContext());
        //JSONObject json_user = json.getJSONObject("user");
        /*sharedPreferences.edit().putBoolean("user_logged_in", true).commit();*/
        //startActivityForResult(new Intent(Polling.this, LOGIN_DESTINATION), LOGIN_REQUEST_CODE);
    }

    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //outState.putInt("tab", getSupportActionBar().getSelectedNavigationIndex());
    }

    public void onResume() {
        super.onResume();
        Log.v(TAG, "onResume");

    }

    protected void onPause() {
        super.onPause();    
        Log.d("Econ", "onpause");
    }

    public static class TabsAdapter extends FragmentPagerAdapter
    implements ActionBar.TabListener, ViewPager.OnPageChangeListener {
        private final Context mContext;
        private final ActionBar mActionBar;
        private final ViewPager mViewPager;
        private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

        static final class TabInfo {
            private final Class<?> clss;
            private final Bundle args;

            TabInfo(Class<?> _class, Bundle _args) {
                clss = _class;
                args = _args;
            }
        }

        public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager) {
            super(activity.getSupportFragmentManager());
            mContext = activity;
            mActionBar = activity.getSupportActionBar();
            mViewPager = pager;
            mViewPager.setAdapter(this);
            mViewPager.setOnPageChangeListener(this);
        }

        public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
            TabInfo info = new TabInfo(clss, args);
            tab.setTag(info);
            tab.setTabListener(this);
            mTabs.add(info);
            mActionBar.addTab(tab);
            notifyDataSetChanged();
        }


        public int getCount() {
            return mTabs.size();
        }

        public SherlockFragment getItem(int position) {
            TabInfo info = mTabs.get(position);
            return (SherlockFragment)Fragment.instantiate(mContext, info.clss.getName(), info.args);
        }


        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }


        public void onPageSelected(int position) {
            mActionBar.setSelectedNavigationItem(position);
        }


        public void onPageScrollStateChanged(int state) {
        }


        public void onTabSelected(Tab tab, FragmentTransaction ft) {
            mViewPager.setCurrentItem(tab.getPosition());
            //Log.v(TAG, "clicked");
            Object tag = tab.getTag();
            for (int i=0; i<mTabs.size(); i++) {
                if (mTabs.get(i) == tag) {
                    mViewPager.setCurrentItem(i);
                }
            }
        }

        public void onTabUnselected(Tab tab, FragmentTransaction ft) {}

        public void onTabReselected(Tab tab, FragmentTransaction ft) {}

        public void onTabReselected(Tab tab, android.app.FragmentTransaction ft) {}

        public void onTabUnselected(Tab tab, android.app.FragmentTransaction ft) {}
    }

LoginFragment:

LoginFragment:

import com.actionbarsherlock.R;
import com.actionbarsherlock.app.SherlockFragment;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class LoginFragment extends SherlockFragment {

    Button loginButton;
    TextView loginErrorMsg;
    Polling activity;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.v("x", "oncreateview");
        View v = inflater.inflate(R.layout.loginfragment, container, false);
        return v;
    }

EconFragment:

EconFragment:

import com.actionbarsherlock.R;
import com.actionbarsherlock.app.SherlockFragment;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class EconFragment extends SherlockFragment {

    private TableLayout questionContainer;
    int pos = 0;
    private String[] titles = {"The first title ", "hallo1","hallo2", "hallo3",
            "hallo4", "hallo5","hallo6", "hallo7","hallo8", "hallo9"};

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.v("Econ", "onCreateView");
        View v = inflater.inflate(R.layout.econfragment, container, false);
        questionContainer = (TableLayout) v.findViewById(R.id.questionContainer);
        //bs
        int leftMargin=5;
        int topMargin=5;
        int rightMargin=5;
        int bottomMargin=5;
        while (pos < 10) {
        View question = inflater.inflate(R.layout.question, null);
        question.setId(pos);
        TextView title = (TextView) question.findViewById(R.id.questionTextView);
        title.setText(titles[pos]);
        Button charts = (Button) question.findViewById(R.id.chartsButton);
        charts.setId(pos);
        charts.setOnClickListener(chartsListener);
        TableRow tr = (TableRow) question;
        TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(
                TableLayout.LayoutParams.MATCH_PARENT,
                TableLayout.LayoutParams.WRAP_CONTENT);
        trParams.setMargins(leftMargin, topMargin, rightMargin, bottomMargin);
        tr.setLayoutParams(trParams);
        Log.v("econ", "while loop");
        questionContainer.addView(tr);
        pos++;
        }

        return v;
    }

和两个XML文件; loginfragment.xml和econfragment.xml:

And the two XML files; loginfragment.xml and econfragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/loginScrollView">
<TableLayout 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/logoImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/logo" 
        android:contentDescription="@string/app_name"
        />

    <TextView
        android:id="@+id/demoTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/logDetails" android:gravity="center_horizontal|center">

        </TextView>

    <EditText
        android:id="@+id/emailEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/loginHint"
        android:imeOptions="actionNext"
        android:inputType="textEmailAddress" android:padding="10dp"/>

    <EditText
        android:id="@+id/passEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/password"
        android:imeOptions="actionDone"
        android:inputType="textPassword" android:padding="10dp"/>

    <Switch
        android:id="@+id/rememberSwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/rememberDetails" android:padding="10dp" android:gravity="right"/>

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <Button
            android:id="@+id/registerButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="2"
            android:padding="5dp"
            android:text="@string/register" />

        <Button
            android:id="@+id/loginButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_weight="2"
            android:padding="5dp"
            android:text="@string/login" />
    </TableRow> 

</TableLayout>
</ScrollView>

econfragment.xml:

econfragment.xml:

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

   <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TableLayout 
            android:id="@+id/questionContainer"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        </TableLayout>
     </ScrollView>

我是否应该做一些根本不同的事情?我应该注意,此错误 only 仅发生在Gingerbread或更低的版本上-例如,在ICS上运行良好。我使用ABS的全部目的是为了获得向后兼容性,所以我现在对此错误一无所知!

Is there something that I should be doing fundamentally differently? I should note that this error only occurs on Gingerbread or lower - it works perfectly fine on ICS, for example. The whole point of me using ABS is to get the backwards compatibility, so I'm a bit nonplussed with this error at the moment!

推荐答案

http://developer.android.com/reference/android/widget/ Switch.html


原因:API级别14

Since: API Level 14

Switch 小部件是在ICS中引入的。

The Switch widget was introduced in ICS.

这篇关于错误夸大类开关ActionBarSherlock(InflateException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆