我从Udacity视频中多次检查并尝试做同样的事情但listview没有显示。你可以检查一下我的代码并告诉我,如果我错过了什么吗?
MainActivity.java
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.container, 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.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();
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);
/*//My way
ArrayList<String> fakeData = new ArrayList<String>();
fakeData.add("Today-Sunny 50/60");
fakeData.add("Tomorrow-Cloudy 20/30");
fakeData.add("Wednesday-Snowy 40/50");
fakeData.add("Thursday-Rainy 20/40"); */
//Google's way
String[] forecastArray={
"Today-Sunny 50/60",
"Tomorrow-Cloudy 20/30",
"Wednesday-Snowy 40/50",
"Thursday-Rainy 20/40",
"Friday-Funny 20/50",
"Sat-Sunny 70/80",
"Sun-Sunny 90/100"
};
ArrayList<String> weekForecast=new ArrayList<String>();
Arrays.asList(weekForecast);
ArrayAdapter adapter = new ArrayAdapter<String>
(getActivity(), R.layout.list_item_forecast, R.id.list_item_forecast_textview, weekForecast);
ListView listview = (ListView) rootView.findViewById(R.id.listview_forecast);
listview.setAdapter(adapter);
return rootView;
}
}
}
activity_main.xml中
<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"/>
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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview_forecast" />
</FrameLayout>
list_item_forecast.xml
<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">
</TextView>
答案 0 :(得分:2)
问题是weekForecast已声明但未初始化。替换:
ArrayList<String> weekForecast=new ArrayList<String>();
Arrays.asList(weekForecast);
使用:
ArrayList<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray));