1

Donc, je me heurte à une erreur. J'ai une SherlockFragmentActivity, et au moins deux SherlockFragments. Les deux utilisent onCreateView pour gonfler un fichier xml. LoginFragment, cependant, a un widget de commutation: le fragment econ ne le fait pas. Si j'ajoute un widget switch dans econfragment.xml, cela provoque un plantage du même type (alors que loginfragment est mis en commentaire hors implémentation). Si je supprime le widget de commutateur de loginfragment, une erreur similaire s'affiche pour RadioGroup - mais econfragment possède déjà un groupe radio et ne génère aucune erreur. Voici le code correspondant:Commutateur de classe de gonflage Erorr, ActionBarSherlock (InflateException)

Polling:

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

Et les deux fichiers XML; loginfragment.xml et 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> 

Y at-il quelque chose que je devrais faire fondamentalement différemment? Je devrais noter que cette erreur seulement se produit sur Gingerbread ou inférieur - il fonctionne parfaitement bien sur ICS, par exemple. Le but de mon utilisation de l'ABS est d'obtenir la rétrocompatibilité, donc je suis un peu interloqué avec cette erreur pour le moment!

Répondre

3

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

depuis: API Niveau 14

Le widget Switch a été introduit dans ICS.

+0

C'est logique - c'est pourquoi j'ai enlevé le commutateur du XML en espérant résoudre le problème. Voici l'erreur qui apparaît une fois que je le fais: java.lang.ClassCastException: android.widget.RadioGroup AndroidRuntime (1459): à com.davekelley.polling.EconFragment. onCreateView (EconFragment.java:46) – Davek804

+0

Votre fichier 'R.java' n'est pas synchronisé. Projet> Propre –

+0

Man ..... J'avais essayé de nettoyer le projet plusieurs fois, mais je n'avais jamais essayé de retirer l'interrupteur, de le nettoyer, puis de le faire fonctionner. C'était toujours propre, enlever l'interrupteur, courir. Cela a résolu le problème - je vais juste devoir utiliser une case à cocher pour l'implémentation des détails du rappel, même si Android Design dit que c'est un nono! Je suppose qu'ils devront réparer leurs malheurs API tôt ou tard. Merci beaucoup Jake, prends un peu plus de rep de moi: P – Davek804

Questions connexes