2016-07-16 7 views
0

udacity.comでAndroidデベロッパートレーニングを行い、日照アプリの実装に続いています。実装にはAndroidスタジオの最新バージョンを使用しています。Udacity Sunshine 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> 

私はあなたの問題は、あなたが実際にエリック・B.で指摘したように、人はいない正しく(動的に追加ここでは2つのフラグメント、1はXMLで宣言を作成し、別の(空)ということであると信じてミー

+1

この時点でフラグメントを使用しないでください。シンプルな空のアクティビティを使用し、コードをonCreateメソッドのフラグメントのonCreateViewの内側に配置します。断片を扱い、そのライフサイクルを管理することは頭痛になる可能性があります。 –

答えて

1

https://developer.android.com/training/basics/fragments/creating.html

は、実行時にフラグメントを追加します。添付されたリンクは関連する支店用です。あなたの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を渡しているので、内部ではレンダリングできません。

関連する問題