2016-07-16 6 views
0

Ich bin durch die Android-Dev-Ausbildung bei Udacity.com, gefolgt zusammen mit der Umsetzung der Sonnenschein app.Ich benutze Android Studio neueste Version für die Umsetzung.Udacity Sunshine App-Lektion 1

Ich bin an dem Punkt zu bekommen, wo ich die Mock Listview bekommen soll und ich bekomme nichts auf dem Bildschirm und es zeigte keine Fehler.

Hier ist mein mainActivity.java Code

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v7.app.ActionBarActivity; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

import java.util.ArrayList; 
import java.util.Arrays; 
import java.util.List; 


public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     if (savedInstanceState == null) { 
      getSupportFragmentManager().beginTransaction() 
        .add(R.id.fragment, new PlaceholderFragment()) 
        .commit(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 

     public PlaceholderFragment() { 

     } 
     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
           Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_main, container, false); 


      String[] foreastarray={ 
        "SUN-CLOUDY","MON-SUNNY","TUE-FOGGY","WED-CLOUDY","THU_ASTEROIDS","FRI-HEAVYRAIN","SAT-SUNNY" 

      }; 
      List<String> weakforecast=new ArrayList<String>(Arrays.asList(foreastarray)); 
      ArrayAdapter<String> mForecastAdapter=new ArrayAdapter<String>(getActivity(), 
        R.layout.list_item_forecast,weakforecast); 



      ListView listView=(ListView) rootView.findViewById(R.id.listview_forecast); 
      listView.setAdapter(mForecastAdapter); 

      return rootView; 
     } 
    } 
} 

Hier Code meine content_main.xml ist

 <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/fragment" 
    android:name="com.example.android.sunshine.MainActivityFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:layout="@layout/fragment_main" /> 

Hier mein fragment_main.xml Code hier ist

 <FrameLayout   xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     tools:context="com.example.android.sunshine.MainActivityFragment" 
     tools:showIn="@layout/activity_main"> 

    <ListView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/listview_forecast"> 

    </ListView> 

    </FrameLayout> 

Schließlich ist mein list_item_forecast.xml Code

Bitte helfen Sie mir

+1

Schauen Sie sich meinen Vorschlag an, verwenden Sie keine Fragmente an dieser Stelle. Verwenden Sie eine einfache leere Aktivität und fügen Sie den Code in die OnCreate-Methode der Aktivität onCreateView des Fragments ein. Das Arbeiten mit Fragmenten und die Verwaltung ihres Lebenszyklus könnten Kopfschmerzen bereiten. –

Antwort

0

Ich glaube, Ihr Problem ist, dass Sie tatsächlich zwei Fragmente erstellen hier eine in XML erklärt, und eine andere (leer) ein dynamisch hinzugefügt (nicht richtig, wie von Eric B. wies darauf hin, in seine Antwort) zur Laufzeit, die das erste Fragment verbirgt.

Sie sollten versuchen, die Transaktion im OnCreate() Ihrer Aktivität zu entfernen.

ein Fragment in XML-Deklaration: https://developer.android.com/training/basics/fragments/creating.html

ein Fragment zur Laufzeit hinzufügen: https://developer.android.com/training/basics/fragments/fragment-ui.html

1

Sie sollten einen Blick auf den Code here nehmen. Der angehängte Link ist für die jeweilige Branche.

In Ihrem content_main.xml, sollten Sie nur eine FrameLayout haben, wie folgt aus:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    tools:context=".MainActivity" tools:ignore="MergeRootFrame" /> 

dieses alles zu tun, an Ort und Stelle fallen.

Der Grund, warum Sie nichts sehen, ist, weil das Fragment in einen Container geladen werden soll, der normalerweise ein FrameLayout ist. Wenn Sie add(R.id.fragment, new PlaceholderFragment()) aufrufen, fügen Sie das Fragment tatsächlich zu dem Container hinzu, dessen ID in dem ersten Parameter übergeben wird, der wie bereits erwähnt ein FrameLayout ist. In Ihrem Fall übergeben Sie eine ID eines Fragments, sodass es nicht darin gerendert werden kann.

Verwandte Themen