我正在尝试按照Udacity开发Android应用程序课程。我得到了课程3.03。在此,我们被要求在点击列表中的项目时从DetailActivity
启动新的活动(ForecastFragment
)。
但是,在尝试启动DetailActivity
时,我一直收到错误消息。 Binary XML file line #1: Error inflating class fragment
,DetailActivity.java
方法onCreate()
引起的。{/ 1}。
我已经四处搜索了,但是没有一个针对这个问题的修正案对我有帮助。
具体错误是:
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class
com.myapp.sunshine.app.DetailActivityFragment that is not a Fragment
DetailActivity.java
public class DetailActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail); //failing here
}
}
DetailActivityFragment.java
public class DetailActivityFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_detail, container, false);
}
}
activity_detail.xml
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
android:name="com.myapp.sunshine.app.DetailActivityFragment"
tools:layout="@layout/fragment_detail" android:layout_width="match_parent"
android:layout_height="match_parent" />
我应该提一下,我不使用支持库 - 因为我的目标是SDK 21+,仅限Lollipop。
这不是我们在课程中创建的唯一片段。我们创建的另一个片段看起来与此相同。
activity_main.xml中
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment"
android:name="com.myapp.sunshine.app.ForecastFragment"
tools:layout="@layout/fragment_main" android:layout_width="match_parent"
android:layout_height="match_parent" />
好的方面,我包括ForecastFragment
课程。我省略了不必要的代码部分。
ForecastFragment.java
public class ForecastFragment extends Fragment {
private ArrayAdapter<String> mForecaseAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] forecastArray = {
"Today - Sunny - 88/63",
"Tomorrow - Foggy - 74/58",
"Sunday - Rainy - 65/59",
"Monday - Cloudy - 72/67"
};
final List<String> weekForecast = new ArrayList<>(Arrays.asList(forecastArray));
mForecaseAdapter = new ArrayAdapter<>(
getActivity(),
R.layout.list_item_forecast,
R.id.list_item_forecast_textview,
weekForecast);
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
final ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
listView.setAdapter(mForecaseAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String forecast = mForecaseAdapter.getItem(position);
Intent detailActivityIntent = new Intent(getActivity(), DetailActivity.class)
.putExtra(Intent.EXTRA_TEXT, forecast);
startActivity(detailActivityIntent);
}
});
return rootView;
}
}
fragment_detail.xml
<RelativeLayout 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=".DetailActivityFragment">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
几乎与
相同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=".MainActivityFragment">
<ListView
android:id="@+id/listview_forecast"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" />
</FrameLayout>
答案 0 :(得分:3)
您应该使用FragmentActivity
代替Activity
public class DetailActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail); //failing here
}
}