Autore Topic: Il Fragment non crea la vista  (Letto 619 volte)

Offline valec

  • Nuovo arrivato
  • *
  • Post: 4
  • Respect: 0
    • Mostra profilo
Il Fragment non crea la vista
« il: 19 Dicembre 2013, 13:24:31 CET »
0
Salve,
sono nuova nella programmazione di app per android e ho qualche problema con i Fragment.
Quello che mi servirebbe è avere tre fragment disposti su un'activity contenitore. Per ora sto provando con uno...
Ho trovato un pò di guide qui e la...ma non ho risolto molto...
vi posto il mio codice

fragment_lecture.xml
Codice: [Seleziona]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
   
       <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_toLeftOf="@+id/button1"
        android:cursorVisible="true"
        android:ems="9"
        android:focusable="true"
        android:focusableInTouchMode="false"
        android:hint="Pronto alla lettura..."
        android:inputType="textMultiLine"
        android:lines="6" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText1"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="16dp"
        android:text="Invia" />
   

</RelativeLayout>

activity_body.xml
Codice: [Seleziona]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <fragment
        android:id="@+id/lecture_f"
        android:name="com.example.erredimobile_fragment.LectureFragment"
        android:tag="lecture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

LectureFragment.java
Codice: [Seleziona]
        @Override
                public void onCreate(Bundle savedInstanceState) {
                        // TODO Auto-generated method stub
                        super.onCreate(savedInstanceState);
                }

                public static LectureFragment newInstance() {
                        LectureFragment fragment = new LectureFragment();
                        return fragment;
                }

                public LectureFragment() {
                        // Required empty public constructor
                }




        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onActivityCreated(savedInstanceState);
        }



        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                View view = inflater.inflate(R.layout.fragment_lecture, container, false);
               
                }
               

contenctActivity.java
Codice: [Seleziona]
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                setContentView(R.layout.activity_body); 
                super.onCreate(savedInstanceState);
                LectureFragment mylecture = new LectureFragment();
        mylecture.setArguments(getIntent().getExtras());
        getFragmentManager().beginTransaction().add(android.R.id.content, mylecture).commit();
       
        }
       
         

        public ContentActivity() {
                // TODO Auto-generated constructor stub
        }


questa è una riga di codice che il log mi riporta
Codice: [Seleziona]
setContentView(R.layout.activity_body); 

il Logcat
Codice: [Seleziona]
12-19 13:05:29.336: E/AndroidRuntime(22792): FATAL EXCEPTION: main
12-19 13:05:29.336: E/AndroidRuntime(22792): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.erredimobile_fragment/com.example.erredimobile_fragment.ContentActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
12-19 13:05:29.336: E/AndroidRuntime(22792):         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at android.app.ActivityThread.access$700(ActivityThread.java:150)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at android.os.Looper.loop(Looper.java:175)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at android.app.ActivityThread.main(ActivityThread.java:5279)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at java.lang.reflect.Method.invokeNative(Native Method)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at java.lang.reflect.Method.invoke(Method.java:511)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at dalvik.system.NativeStart.main(Native Method)
12-19 13:05:29.336: E/AndroidRuntime(22792): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
12-19 13:05:29.336: E/AndroidRuntime(22792):         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at android.app.Activity.setContentView(Activity.java:1930)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at com.example.erredimobile_fragment.ContentActivity.onCreate(ContentActivity.java:16)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at android.app.Activity.performCreate(Activity.java:5283)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
12-19 13:05:29.336: E/AndroidRuntime(22792):         ... 11 more
12-19 13:05:29.336: E/AndroidRuntime(22792): Caused by: java.lang.IllegalStateException: Fragment com.example.erredimobile_fragment.LectureFragment did not create a view.
12-19 13:05:29.336: E/AndroidRuntime(22792):         at android.app.Activity.onCreateView(Activity.java:4895)
12-19 13:05:29.336: E/AndroidRuntime(22792):         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
12-19 13:05:29.336: E/AndroidRuntime(22792):         ... 21 more

Grazie a tutti in anticipo
« Ultima modifica: 20 Dicembre 2013, 13:46:41 CET da valec »

iClaude

  • Visitatore
Re:Il Fragment non crea la vista
« Risposta #1 il: 19 Dicembre 2013, 19:26:44 CET »
0
Non ho letto tutto il codice, ma la prima cosa che salta all'occhio è che nel metodo onCreateView del Fragment manca return view, in pratica quel metodo non sembra restituire niente.
Darei anche una controllata all'xml del Fragment (primo EditText): cos'è quel request focus?

Offline valec

  • Nuovo arrivato
  • *
  • Post: 4
  • Respect: 0
    • Mostra profilo
Re:Il Fragment non crea la vista
« Risposta #2 il: 20 Dicembre 2013, 09:27:43 CET »
0
grazie per la risopsta...
ho tolto  il request focus che mi era sfuggito...ma non funziona cmq
poi ho sbagliato a copiare la create view...c'è il return.... vi posto tutto il metodo

Codice: [Seleziona]
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                View view = inflater.inflate(R.layout.fragment_lecture, container, false);
               
                final EditText codici = (EditText) getView().findViewById(R.id.editText1);
                Button button = (Button) getView().findViewById(R.id.button1);
                button.setOnClickListener(this);
                //final EditText codici = (EditText) findViewById(R.id.editText1);
                codici.setFocusable(true);
                codici.requestFocus();
                codici.setCursorVisible(true);
                calendar.add(Calendar.HOUR, -24);
                calendar.add(Calendar.HOUR_OF_DAY, -24);

                codici.setOnKeyListener(new OnKeyListener() {
                int count=0;
               
       
                int giorno = calendar.getInstance().get(Calendar.DAY_OF_MONTH);
                int mese = calendar.getInstance().get(Calendar.MONTH)+1;
                int anno = calendar.getInstance().get(Calendar.YEAR);
               
               
       
               
               
                   public boolean onKey(View v, int keyCode, KeyEvent event) {
                   String data_ora;
                        if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                            (keyCode == KeyEvent.KEYCODE_ENTER))
                        {
                                int ore = calendar.getInstance().get(Calendar.HOUR_OF_DAY);
                                         int minuti = calendar.getInstance().get(Calendar.MINUTE);
                                         int secondi = calendar.getInstance().get(Calendar.SECOND);
                                count++;
                                if(count%2==0)
                                {
                                         data_ora=";"+giorno+"-"+mese+"-"+anno+" ore "+ore+":"+minuti+":"+secondi+"\n";
                                         codici.append(data_ora);
                                         contaTAG++;
                                         if(contaTAG==10)
                                         {
                                                 scriviFile(codici);
                                                 contaTAG=0;
                                                 codici.setText("");
                                         }
                                }
                               
                                return true;
                        }
                        return false;
                    }
                });

                return view;
        }

vi riposto il logcat:
Codice: [Seleziona]
12-20 09:46:09.349: E/AndroidRuntime(1510): FATAL EXCEPTION: main
12-20 09:46:09.349: E/AndroidRuntime(1510): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.erredimobile_fragment/com.example.erredimobile_fragment.ContentActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
12-20 09:46:09.349: E/AndroidRuntime(1510):         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at android.app.ActivityThread.access$700(ActivityThread.java:150)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at android.os.Looper.loop(Looper.java:175)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at android.app.ActivityThread.main(ActivityThread.java:5279)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at java.lang.reflect.Method.invokeNative(Native Method)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at java.lang.reflect.Method.invoke(Method.java:511)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at dalvik.system.NativeStart.main(Native Method)
12-20 09:46:09.349: E/AndroidRuntime(1510): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
12-20 09:46:09.349: E/AndroidRuntime(1510):         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at android.app.Activity.setContentView(Activity.java:1930)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at com.example.erredimobile_fragment.ContentActivity.onCreate(ContentActivity.java:17)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at android.app.Activity.performCreate(Activity.java:5283)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
12-20 09:46:09.349: E/AndroidRuntime(1510):         ... 11 more
12-20 09:46:09.349: E/AndroidRuntime(1510): Caused by: java.lang.IllegalStateException: Fragment com.example.erredimobile_fragment.LectureFragment did not create a view.
12-20 09:46:09.349: E/AndroidRuntime(1510):         at android.app.Activity.onCreateView(Activity.java:4895)
12-20 09:46:09.349: E/AndroidRuntime(1510):         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
12-20 09:46:09.349: E/AndroidRuntime(1510):         ... 21 more

e il manifest
Codice: [Seleziona]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.erredimobile_fragment"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="14" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.INTERNET"/>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
      <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_LOCATION" />
        <uses-permission android:name="android.permission.ACCESS_GPS" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
   

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppBaseTheme" >
        <activity
            android:name="com.example.erredimobile_fragment.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity
            android:name="com.example.erredimobile_fragment.ContentActivity">
            </activity>
            <fragment
               android:name="com.example.erredimobile_fragment.LectureFragment">
            </fragment>
    </application>

</manifest>
« Ultima modifica: 20 Dicembre 2013, 09:48:29 CET da valec »

iClaude

  • Visitatore
Re:Il Fragment non crea la vista
« Risposta #3 il: 20 Dicembre 2013, 09:53:57 CET »
0
Ancora una volta non ho letto tutto il codice, ma nel metodo onCreate prima devi richiamare il costruttore della superclasse con super.onCreate ecc. e dopo chiamare setContentView...

Offline valec

  • Nuovo arrivato
  • *
  • Post: 4
  • Respect: 0
    • Mostra profilo
Re:Il Fragment non crea la vista
« Risposta #4 il: 20 Dicembre 2013, 10:06:03 CET »
0
grazie...cosi facendo non mi da più l'errore nel log
Codice: [Seleziona]
Caused by: java.lang.IllegalStateException: Fragment com.example.erredimobile_fragment.LectureFragment did not create a view.
ma ancora non va...ecco il logcat appena eseguito
Codice: [Seleziona]
12-20 10:03:24.779: E/AndroidRuntime(10645): FATAL EXCEPTION: main
12-20 10:03:24.779: E/AndroidRuntime(10645): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.erredimobile_fragment/com.example.erredimobile_fragment.ContentActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.app.ActivityThread.access$700(ActivityThread.java:150)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.os.Looper.loop(Looper.java:175)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.app.ActivityThread.main(ActivityThread.java:5279)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at java.lang.reflect.Method.invokeNative(Native Method)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at java.lang.reflect.Method.invoke(Method.java:511)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at dalvik.system.NativeStart.main(Native Method)
12-20 10:03:24.779: E/AndroidRuntime(10645): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:350)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.app.Activity.setContentView(Activity.java:1930)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at com.example.erredimobile_fragment.ContentActivity.onCreate(ContentActivity.java:19)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.app.Activity.performCreate(Activity.java:5283)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
12-20 10:03:24.779: E/AndroidRuntime(10645):         ... 11 more
12-20 10:03:24.779: E/AndroidRuntime(10645): Caused by: java.lang.NullPointerException
12-20 10:03:24.779: E/AndroidRuntime(10645):         at com.example.erredimobile_fragment.LectureFragment.onCreateView(LectureFragment.java:108)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.app.Fragment.performCreateView(Fragment.java:1695)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:879)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1053)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1155)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.app.Activity.onCreateView(Activity.java:4872)
12-20 10:03:24.779: E/AndroidRuntime(10645):         at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:686)
12-20 10:03:24.779: E/AndroidRuntime(10645):         ... 21 more

continuo anche  a cercare...
grazie

Post unito: 20 Dicembre 2013, 13:46:27 CET
ciao a tutti

ho risolto...

è bastato mettere

Codice: [Seleziona]
        View view = inflater.inflate(R.layout.fragment_lecture, container, false);
               
                final EditText codici = (EditText) view.findViewById(R.id.editText1);
                Button button = (Button) view.findViewById(R.id.button1);

grazie cmq
« Ultima modifica: 20 Dicembre 2013, 13:46:27 CET da valec, Reason: Merged DoublePost »