2016-12-24 52 views
1

私はUdacityのプロジェクトSunshineから始めました。
私は非常に最初の章にあり、リストビューを作成しようとしています。しかし、私のリストビューにはデータが入っていません。 Genymotionエミュレータで実行すると、アクションバーには空白の画面しか表示されません。断片化していない断片の中の単純なリスト

マイコード:
MainActivity.java

package snehit.sunshine.app; 

import android.app.Fragment; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.Menu; 
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 AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

@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, menu); 
    return true; 
} 

public static class PlaceHolderFragment extends Fragment { 
    @Override 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View rootView = inflater.inflate(R.layout.fragment_main,container,false); 

     String forecastArray[] = { 
       "Today - Sunny - 88/63", 
       "tom - Sunny - 88/63", 
       "day after - Cyclone - 88/63", 
       "another day - Tornadoes - 88/63", 
       "One another day - Sunny - 88/63", 
       "Next day - Sunny - 88/63", 
       "again, next day - Sunny - 88/63" 

     }; 

     List<String> weekForecast = new ArrayList<String>(
       Arrays.asList(forecastArray) 
     ); 

     ArrayAdapter mForecastAdapter = new ArrayAdapter(getActivity(),R.layout.list_item_forecast, R.id.list_item_forecast_textview,weekForecast); 

     ListView listview = (ListView) rootView.findViewById(R.id.listview_forecast); 
     listview.setAdapter(mForecastAdapter); 
     return rootView; 
    } 
} 
} 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<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" 
    tools:ignore="MergeRootFrame" 
    android:id="@+id/container" 
    tools:context="snehit.sunshine.app.MainActivity"> 

</FrameLayout> 

fragment_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:paddingLeft="64dp" 
    android:paddingRight="64dp" 
    android:paddingTop="16dp" 
    android:paddingBottom="16dp" 
    tools:context=".MainActivity$PlaceHolderFragment" 
    android:layout_height="match_parent"> 

    <ListView 
     android:layout_width="match_parent" 
     android:id="@+id/listview_forecast" 
     android:layout_height="match_parent" /> 
</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= "wrap_content" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:gravity="center_vertical" 
    android:id="@+id/list_item_forecast_textview" 
    /> 

私はAndroidスタジオ2.1を使用しています(Eclipseを使用して変更を加えることができないかどうかはわかりません...私はアンドロイド開発に新しいです)IDEはエラーを表示せず、アプリケーションがクラッシュしていません開始しました。私はMarshmallow(API 23)を実行しているGenymotionエミュレータでテストしました

答えて

1

を私はあなたのコードの下に試してみてくださいFrameLayout でフラグメントをロードすることを忘れ考える

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_stack_main" 
    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.mahii.notifybox.StackMainActivity"> 

    <FrameLayout 
     android:id="@+id/container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</RelativeLayout> 

断片クラス

public class StackFragment extends Fragment { 

    ListView listview_forecast; 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.fragment_stack, container, false); 

     listview_forecast = (ListView) view.findViewById(R.id.listview_forecast); 

     String forecastArray[] = { 
       "Today - Sunny - 88/63", 
       "tom - Sunny - 88/63", 
       "day after - Cyclone - 88/63", 
       "another day - Tornadoes - 88/63", 
       "One another day - Sunny - 88/63", 
       "Next day - Sunny - 88/63", 
       "again, next day - Sunny - 88/63" 

     }; 

     List<String> weekForecast = new ArrayList<>(
       Arrays.asList(forecastArray) 
     ); 

     ArrayAdapter mForecastAdapter = new ArrayAdapter(getActivity(), R.layout.list_item_forecast, R.id.list_item_forecast_textview, weekForecast); 
     listview_forecast.setAdapter(mForecastAdapter); 

     return view; 

    } 
} 

fragment_stack.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

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

</LinearLayout> 

そして最後に、あなたのMainActivity

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_stack_main); 

    StackFragment stackFragment = new StackFragment(); 
    FragmentManager fm = getSupportFragmentManager(); 
    FragmentTransaction ft = fm.beginTransaction(); 
    ft.replace(R.id.container, stackFragment, stackFragment.getClass().getName()); 
    ft.commit(); 

} 

ここに出力があります

enter image description here

0

あなたがペーストしたコードに従って、あなたのアクティビティにフラグメントを膨らませていないことを意味するあなたのアクティビティの断片トランザクションは見えません。 あなたが直接あなたのacvityのXMLであなたのPlaceHolderFragmentを膨らませることができ、それ

  1. に2つのオプションがあります。
  2. FragmentManagerを使用して、Fragmentをアクティビティのビューに追加できます。

詳細は、リンクを参照してください:Fragment in Activity

おかげで、よろしく