我正在尝试开发一个包含Fragment的简单应用程序(我第一次使用它)。问题是我的活动主要的XML没有显示标题栏,也许我正在做片段的错误,无法理解什么。
MainActivity.java:
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(savedInstanceState==null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment, new ForecastFragment()).commit();
}
}
}
activity_main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.sgrumo.sunshineonyourmind.app.Activities.ForecastFragment"
tools:context=".app.Activities.ForecastFragment">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
android:name="com.example.sgrumo.sunshineonyourmind.app.Activities.ForecastFragment"
tools:layout="@layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
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: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=".ForecastFragment"
tools:showIn="@layout/activity_main">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listview_forecast"
android:layout_gravity="center" />
content_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_forecast"
android:name="com.example.sgrumo.sunshineonyourmind.app.Activities.ForecastFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
ForecastFragment.java
public class ForecastFragment extends Fragment {
public ForecastFragment() {}
String[] forecastArray = {
"Today - Sunny - 31/20",
"Tomorrow - Sunny - 42,30",
"Wednesday - Rainy - 20,15",
"Thursday - FINIMONDO - 35,20"
};
ArrayList<String> list = new ArrayList<>(Arrays.asList(forecastArray));
@Override
public void onCreate(Bundle savedIstanceState ){
super.onCreate(savedIstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,false);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),R.layout.list_item_forecast,R.id.list_item_forecast_textview,list);
ListView lw = (ListView)rootView.findViewById(R.id.listview_forecast);
lw.setAdapter(adapter);
return rootView;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater ) {
inflater.inflate(R.menu.forecastfragment, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if( id == R.id.action_refresh ) {
FetchWeatherTask fetchWeatherTask = new FetchWeatherTask();
fetchWeatherTask.execute();
return true;
}
return super.onOptionsItemSelected(item);
}
}
logcat on start:
08-10 12:22:55.397 2311-2311/com.example.sgrumo.sunshineonyourmind I/art: Not late-enabling -Xcheck:jni (already on)
08-10 12:22:55.397 2311-2311/com.example.sgrumo.sunshineonyourmind W/art: Unexpected CPU variant for X86 using defaults: x86
08-10 12:22:55.433 2311-2311/com.example.sgrumo.sunshineonyourmind W/System: ClassLoader referenced unknown path: /data/app/com.example.sgrumo.sunshineonyourmind-1/lib/x86
08-10 12:23:01.545 2311-2311/com.example.sgrumo.sunshineonyourmind W/System: ClassLoader referenced unknown path: /data/app/com.example.sgrumo.sunshineonyourmind-1/lib/x86
08-10 12:23:01.725 2311-2747/com.example.sgrumo.sunshineonyourmind I/OpenGLRenderer: Initialized EGL, version 1.4
08-10 12:23:01.725 2311-2747/com.example.sgrumo.sunshineonyourmind D/OpenGLRenderer: Swap behavior 1
答案 0 :(得分:0)
在花了几个小时试图调试我的应用程序之后,我再次开始选择一个空活动的项目。现在一切都很完美!