2016-07-16 50 views
0

我正在通過udacity.com上的android dev培訓,以下是陽光app的實施。我正在使用android studio最新版本進行實施。Udacity陽光app-lesson 1

我想要得到模擬列表視圖的位置,而且屏幕上什麼都沒有,並且顯示沒有錯誤。

這裏是我的mainActivity.java代碼

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

這裏是我的content_main.xml代碼

 <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" /> 

這裏是我的fragment_main.xml代碼

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

最後這裏是我的list_item_forecast.xml代碼

<?xml version="1.0" encoding="utf-8"?> 
    <TextView xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/list_item_forecast_textview" 
     android:gravity="center_vertical"> 
    </TextView> 

請幫我

+1

看看我對你的建議是不要在這一點上使用片段。在活動的onCreate方法中使用簡單的空活動並將代碼放入片段的onCreateView中。處理碎片並管理其生命週期可能會令人頭疼。 –

回答

0

我相信你的問題是,你居然在這裏創建2個片段,在XML聲明的一個,另一個(空)一個動態添加(不正確,如指出,由Eric B.他的回答)在運行時隱藏了第一個片段。

您應該嘗試刪除活動的onCreate()內的事務。

中聲明XML片段:https://developer.android.com/training/basics/fragments/creating.html

在運行時添加一個片段:https://developer.android.com/training/basics/fragments/fragment-ui.html

1

你應該看看代碼here。所附的鏈接是針對相關分支的。

在你content_main.xml,你應該有一個FrameLayout裏,像這樣的:

<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" /> 

通過這樣做,一切都會順利到位。

您看不到任何東西的原因是因爲該片段將被加載到一個容器中,該容器通常是一個FrameLayout。當你調用add(R.id.fragment, new PlaceholderFragment())時,你實際上將片段添加到在第一個參數中傳遞id的容器中,正如我前面所說的那樣,它是一個FrameLayout。在你的情況下,你傳遞一個片段的ID,所以它無法在其中渲染。