gpt4 book ai didi

android - 错误膨胀类开关,ActionBarSherlock (InflateException)

转载 作者:行者123 更新时间:2023-11-29 18:12:26 24 4
gpt4 key购买 nike

我遇到了一个错误的痛苦。我有一个 SherlockFragmentActivity 和至少两个 SherlockFragment。两者都使用 onCreateView 来扩充 xml 文件。然而,LoginFragment 有一个开关小部件:econ fragment 没有。如果我将开关小部件添加到 econfragment.xml 中,则会导致相同类型的崩溃(而 loginfragment 在实现中被注释掉了)。如果我从 loginfragment 中删除开关小部件,则 RadioGroup 会出现类似的错误 - 但 econfragment 已经有一个 radio 组并且不会产生任何错误。相关代码如下:

投票:

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) {}
}

登录 fragment :

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;
}

经济 fragment :

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:

<?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:

<?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>

有什么我应该做的完全不同的事情吗?我应该注意,此错误 发生在 Gingerbread 或更低版本上 - 例如,它在 ICS 上工作得很好。我使用 ABS 的全部目的是为了获得向后兼容性,所以我现在对这个错误有点不知所措!

最佳答案

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

Since: API Level 14

ICS 中引入了 Switch 小部件。

关于android - 错误膨胀类开关,ActionBarSherlock (InflateException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9961337/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com