我是Android新手。我试图使用片段上的适配器从应用程序中的String数组显示一个简单的预测。它没有出现在窗户上。我已经定义了2个类(MainActivity和MainActivityFragment)。我使用的是Android Studio 2.1.3。
如果我在activity_mail.xml中使用片段而不是ListView,则会给我一个stackoverflow错误。
如果我在activity_mail.xml上使用ListView,它就不会出现。
任何帮助,都会非常感激
这是我的代码:
a)MainActivity.java
package app.com.example.android.sunshine3;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
b)MainActivityFragment.java
package app.com.example.android.sunshine3;
import android.app.Fragment;
import android.os.Bundle;
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 MainActivityFragment extends Fragment {
public MainActivityFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ArrayAdapter<String> mForecastAdapter;
// 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.
mForecastAdapter =
new ArrayAdapter<String>(
getActivity(), // The current context (this activity)
R.layout.list_item_forescast, // The name of the layout ID.
R.id.list_item_forecast_textview, // The ID of the textview to populate.
weekForecast);
// Inflate the root layout for this fragment
View rootView = inflater.inflate(R.layout.activity_main, container, false);
// Get a reference to the ListView, and attach this adapter to it.
ListView listView = (ListView) rootView.findViewById(R.id.fragment);
listView.setAdapter(mForecastAdapter);
return rootView;
}
}
c)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/container"
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="app.com.example.android.sunshine3.MainActivity">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment"
android:name="app.com.example.android.sunshine3.MainActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
d)list_item_forescast.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_item_forecast_textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"></TextView>
答案 0 :(得分:0)
问题是您并没有真正使用您的片段,因为您没有在activity
中添加它。一个简单的解决方法是为您的活动定义一个独立的布局:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/fragment_container"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</FrameLayout>
现在,在您的活动中,将fragment
添加到此容器中:
setContentView(R.layout.home_activity_layout);
MainActivityFragment myFragment = new MainActivityFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().add(R.id.fragment_container, myFragment, MainActivityFragment.class.getName()).commit();
应该这样做。