1

所以,我遇到了一個錯誤的痛苦。我有一個SherlockFragmentActivity,並且至少有兩個SherlockFragments。兩者都使用onCreateView來擴充xml文件。然而,LoginFragment有一個開關小部件:econ片段沒有。如果我在econfragment.xml中添加一個切換小部件,會導致相同類型的崩潰(而loginfragment被註釋爲執行不足)。如果我從loginfragment中刪除開關小部件,RadioGroup會出現類似的錯誤 - 但econfragment已經有一個廣播組,並且不會產生錯誤。下面是相關代碼:Erorr充氣類開關,ActionBarSherlock(InflateException)

投票:

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:

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:

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> 

有什麼,我應該從根本上不同的做什麼?我應該注意到,這個錯誤只有發生在薑餅或更低 - 例如,它在ICS上工作得很好。我使用ABS的重點在於獲得向後兼容性,所以我現在對這個錯誤感到有點不知所措!

回答

3

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

自:API級別14

Switch部件在ICS中引入的。

+0

有意義 - 這就是爲什麼我從希望解決問題的XML中刪除切換。以下是我遇到的錯誤: java.lang.ClassCastException:android.widget.RadioGroup AndroidRuntime(1459):at com.davekelley.polling.EconFragment。 onCreateView(EconFragment.java:46) – Davek804 2012-04-01 02:37:13

+0

您的'R.java'文件不同步。項目>清潔 – 2012-04-01 02:41:33

+0

男人.....我曾嘗試清理項目多次,但從未嘗試刪除開關,清潔,然後運行。它總是乾淨的,刪除開關,運行。這解決了這個問題 - 即使Android設計說這是一個nono!我只需要使用複選框來記住細節實現。猜猜他們不得不早晚修復他們的API問題。非常感謝傑克,請多給我一些代表:P – Davek804 2012-04-01 02:45:44

相關問題