我已开始使用Sunshine on Udacity项目 我在第一章并试图填充列表视图。但我的列表视图没有填充。在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 Studio 2.1(我不知道如果使用Eclipse会做出任何改变......我是Android开发的新手)IDE没有显示任何错误,也没有应用程序在崩溃时崩溃开始。我在运行Marshmallow(API 23)
的Genymotion模拟器上测试了它答案 0 :(得分: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();
}
这是输出
答案 1 :(得分:0)
根据您粘贴的代码,我无法在您的活动中看到片段交易,这意味着您没有将片段夸大到您的活动中。 它有两个选项
请参阅链接以获取更多信息:Fragment in Activity
谢谢和问候