我正在制作Udacity Sunshine应用程序。由于存在使用的Android Studio版本为0.5.8,现在我使用的是Android Studio 3.0.1。因此,这两种编码技术之间都存在很大差距。
因此,在这里,我在空活动中使用了片段,并通过阅读有关该片段的developer.android.com文章来实现了该片段,并且得到了有关该片段的抽象概念,但其中很多仍不清楚。
因此,我完成了添加片段并将虚假数据添加到“列表视图”以填充它的过程。
但是当我在设备上运行该应用程序时,它仍然显示空白屏幕,并且没有在代码中添加的文本视图:
layout / 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" />
layout / fragment_layout.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"
tools:context=".ArticleFragment"
tools:showIn="@layout/activity_main">
<ListView
android:id="@+id/listview_forecast"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
布局/list_of_item_forecast.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
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"
tools:layout="@layout/fragment_layout"/>
包/MainActivity.java
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_layout);
}
}
包/ArticleFragment.java
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
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 ArticleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create some dummy data for the ListView. Here's a sample weekly forecast
String[] data = {
"Mon 6/23 - Sunny - 31/17",
"Tue 6/24 - Foggy - 21/8",
"Wed 6/25 - Cloudy - 22/17",
"Thurs 6/26 - Rainy - 18/11",
"Fri 6/27 - Foggy - 21/10",
"Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
"Sun 6/29 - Sunny - 20/7"
};
List<String> weekForecast = new ArrayList<String>(Arrays.asList(data));
// Now that we have some dummy forecast data, create an ArrayAdapter.
// The ArrayAdapter will take data from a source (like our dummy forecast) and
// use it to populate the ListView it's attached to.
ArrayAdapter<String> mForecastAdapter =
new ArrayAdapter<String>(
getActivity(), // The current context (this activity)
R.layout.list_item_forecast, // The name of the layout ID.
R.id.list_item_forecast_textview, // The ID of the textview to populate.
weekForecast);
View rootView = inflater.inflate(R.layout.fragment_layout, container, false);
// Get a reference to the ListView, and attach this adapter to it.
ListView listview = (ListView) rootView.findViewById(
R.id.listview_forecast);
listview.setAdapter(mForecastAdapter);
// Inflate the layout for this fragment
return rootView;
}
}
最佳答案
代码的问题是您没有实例化已创建的Fragment类。您需要实例化它并将其传递给FragmentManager
,并且还需要在您的主要活动中更改setContentView(R.layout.fragment_layout)
。
只需将您的MainActivity.java
类更改为此,即可解决问题。无需更改布局文件。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new ArticleFragment())
.commit();
}
}