我的AndroID Studio不支持使用Fragment进行空白活动.
所以,我稍后手动添加它.
但是当我向main_activity.xml添加Fragment引用时,应用程序在模拟器中停止.
主要活动:
package com.example.urvashirohilla.sunshine.app;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;public class MainActivity extends AppCompatActivity { @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); }}
片段类:
package com.example.urvashirohilla.sunshine.app;import androID.content.Context;import androID.net.Uri;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 PlaceholderFragment extends Fragment { // Todo: Rename parameter arguments,choose names that match // the fragment initialization parameters,e.g. ARG_ITEM_NUMBER private static final String ARG_ParaM1 = "param1"; private static final String ARG_ParaM2 = "param2"; // Todo: Rename and change types of parameters private String mParam1; private String mParam2; private OnFragmentInteractionListener mListener; public PlaceholderFragment() { // required empty public constructor } public static PlaceholderFragment newInstance(String param1,String param2) { PlaceholderFragment fragment = new PlaceholderFragment(); Bundle args = new Bundle(); args.putString(ARG_ParaM1,param1); args.putString(ARG_ParaM2,param2); fragment.setArguments(args); return fragment; } @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (getArguments() != null) { mParam1 = getArguments().getString(ARG_ParaM1); mParam2 = getArguments().getString(ARG_ParaM2); } } @OverrIDe public VIEw onCreateVIEw(LayoutInflater inflater,VIEwGroup container,Bundle savedInstanceState) { // Inflate the layout for this fragment // 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)); ArrayAdapter<String> mWeekForecastAdapter = new ArrayAdapter<String>(getActivity(),R.layout.List_item_forecast,R.ID.List_item_forecast_text_vIEw,weekForecast); VIEw rootVIEw = inflater.inflate(R.layout.fragment_placeholder,container,false); ListVIEw ListVIEw = (ListVIEw) rootVIEw.findVIEwByID(R.ID.List_vIEw_forecast); ListVIEw.setAdapter(mWeekForecastAdapter); return rootVIEw; } // Todo: Rename method,update argument and hook method into UI event public voID onbuttonpressed(Uri uri) { if (mListener != null) { mListener.onFragmentInteraction(uri); } } @OverrIDe public voID onAttach(Context context) { super.onAttach(context); if (context instanceof OnFragmentInteractionListener) { mListener = (OnFragmentInteractionListener) context; } else { throw new RuntimeException(context.toString() + " must implement OnFragmentInteractionListener"); } } @OverrIDe public voID onDetach() { super.onDetach(); mListener = null; } public interface OnFragmentInteractionListener { // Todo: Update argument type and name voID onFragmentInteraction(Uri uri); }}
main_activity.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: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.example.urvashirohilla.sunshine.app.MainActivity"><fragment class = "com.example.urvashirohilla.sunshine.app.PlaceholderFragment" androID:ID ="@+ID/fragment_place" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" tools:layout="@layout/fragment_placeholder" /></relativeLayout>
更新Logcat:
09-08 16:58:22.772 2244-2244/com.example.urvashirohilla.sunshine.app E/libprocessgroup: Failed to make and chown /acct/uID_10058: Read-only file system09-08 16:58:22.772 2244-2244/com.example.urvashirohilla.sunshine.app W/Zygote: createProcessGroup Failed,kernel missing CONfig_CGROUP_cpuACCT?09-08 16:58:22.774 2244-2244/com.example.urvashirohilla.sunshine.app I/art: Not late-enabling -Xcheck:jni (already on)09-08 16:58:23.195 2244-2257/com.example.urvashirohilla.sunshine.app I/art: Background sticky concurrent mark sweep GC freed 8208(424KB) AllocSpace objects,5(105KB) LOS objects,39% free,678KB/1117KB,paused 5.661ms total 73.994ms09-08 16:58:25.607 2244-2252/com.example.urvashirohilla.sunshine.app W/art: Suspending all threads took: 28.005ms09-08 16:58:26.088 2244-2257/com.example.urvashirohilla.sunshine.app W/art: Suspending all threads took: 30.701ms09-08 16:58:26.772 2244-2252/com.example.urvashirohilla.sunshine.app W/art: Suspending all threads took: 183.063ms09-08 16:58:26.824 2244-2244/com.example.urvashirohilla.sunshine.app W/art: Before AndroID 4.1,method androID.graphics.PorterDuffcolorFilter androID.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(androID.graphics.PorterDuffcolorFilter,androID.content.res.colorStateList,androID.graphics.PorterDuff$Mode) would have incorrectly overrIDden the package-private method in androID.graphics.drawable.Drawable09-08 16:58:27.106 2244-2252/com.example.urvashirohilla.sunshine.app W/art: Suspending all threads took: 14.965ms09-08 16:58:27.353 2244-2244/com.example.urvashirohilla.sunshine.app D/AndroIDRuntime: Shutting down VM --------- beginning of crash09-08 16:58:27.354 2244-2244/com.example.urvashirohilla.sunshine.app E/AndroIDRuntime: FATAL EXCEPTION: main Process: com.example.urvashirohilla.sunshine.app,PID: 2244 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.urvashirohilla.sunshine.app/com.example.urvashirohilla.sunshine.app.MainActivity}: androID.vIEw.InflateException: Binary XML file line #11: Error inflating class fragment at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2325) at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at androID.app.ActivityThread.access0(ActivityThread.java:151) at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at androID.os.Handler.dispatchMessage(Handler.java:102) at androID.os.Looper.loop(Looper.java:135) at androID.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: androID.vIEw.InflateException: Binary XML file line #11: Error inflating class fragment at androID.vIEw.LayoutInflater.createVIEwFromTag(LayoutInflater.java:763) at androID.vIEw.LayoutInflater.rInflate(LayoutInflater.java:806) at androID.vIEw.LayoutInflater.inflate(LayoutInflater.java:504) at androID.vIEw.LayoutInflater.inflate(LayoutInflater.java:414) at androID.vIEw.LayoutInflater.inflate(LayoutInflater.java:365) at androID.support.v7.app.AppCompatDelegateImplV7.setContentVIEw(AppCompatDelegateImplV7.java:280) at androID.support.v7.app.AppCompatActivity.setContentVIEw(AppCompatActivity.java:140) at com.example.urvashirohilla.sunshine.app.MainActivity.onCreate(MainActivity.java:15) at androID.app.Activity.performCreate(Activity.java:5990) at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2278) at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at androID.app.ActivityThread.access0(ActivityThread.java:151) at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at androID.os.Handler.dispatchMessage(Handler.java:102) at androID.os.Looper.loop(Looper.java:135) at androID.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:698) Caused by: java.lang.RuntimeException: com.example.urvashirohilla.sunshine.app.MainActivity@1bef27fa must implement OnFragmentInteractionListener at com.example.urvashirohilla.sunshine.app.PlaceholderFragment.onAttach(PlaceholderFragment.java:109) at androID.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:1019) at androID.support.v4.app.FragmentManagerImpl.movetoState(FragmentManager.java:1230) at androID.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1332) at androID.support.v4.app.FragmentManagerImpl.onCreateVIEw(FragmentManager.java:2288) at androID.support.v4.app.FragmentController.onCreateVIEw(FragmentController.java:120) at androID.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateVIEw(FragmentActivity.java:356) at androID.support.v4.app.BaseFragmentActivityHoneycomb.onCreateVIEw(BaseFragmentActivityHoneycomb.java:31) at androID.support.v4.app.FragmentActivity.onCreateVIEw(FragmentActivity.java:79) at androID.vIEw.LayoutInflater.createVIEwFromTag(LayoutInflater.java:733) at androID.vIEw.LayoutInflater.rInflate(LayoutInflater.java:806) at androID.vIEw.LayoutInflater.inflate(LayoutInflater.java:504) at androID.vIEw.LayoutInflater.inflate(LayoutInflater.java:414) at androID.vIEw.LayoutInflater.inflate(LayoutInflater.java:365) at androID.support.v7.app.AppCompatDelegateImplV7.setContentVIEw(AppCompatDelegateImplV7.java:280) at androID.support.v7.app.AppCompatActivity.setContentVIEw(AppCompatActivity.java:140) at com.example.urvashirohilla.sunshine.app.MainActivity.onCreate(MainActivity.java:15) at androID.app.Activity.performCreate(Activity.java:5990) at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2278) at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at androID.app.ActivityThread.access0(ActivityThread.java:151) at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at androID.os.Handler.dispatchMessage(Handler.java:102) at androID.os.Looper.loop(Looper.java:135) at androID.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:698)解决方法 看起来你错过了必须在这一行上实现OnFragmentInteractionListener.
public class MainActivity extends FragmentActivity
此外,如果使用支持库,那么AppCompatActivity是推荐的扩展类(它扩展了FragmentActivity本身)
换一种说法,
public class MainActivity extends AppCompatActivity implements OnFragmentInteractionListener
然后,如果您从中获得错误,那么很可能您在该活动的清单中缺少AppCompat主题
总结以上是内存溢出为你收集整理的android – 在Activity中显示片段 – 类无法实例化全部内容,希望文章能够帮你解决android – 在Activity中显示片段 – 类无法实例化所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)