我是Android新手,刚开始学习 我的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 system
09-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.994ms
09-08 16:58:25.607 2244-2252/com.example.urvashirohilla.sunshine.app W/art: Suspending all threads took: 28.005ms
09-08 16:58:26.088 2244-2257/com.example.urvashirohilla.sunshine.app W/art: Suspending all threads took: 30.701ms
09-08 16:58:26.772 2244-2252/com.example.urvashirohilla.sunshine.app W/art: Suspending all threads took: 183.063ms
09-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.Drawable
09-08 16:58:27.106 2244-2252/com.example.urvashirohilla.sunshine.app W/art: Suspending all threads took: 14.965ms
09-08 16:58:27.353 2244-2244/com.example.urvashirohilla.sunshine.app D/AndroidRuntime: Shutting down VM
--------- beginning of crash
09-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.access$800(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.access$800(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.access$800(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)
答案 0 :(得分:2)
看起来你错过了public class MainActivity extends FragmentActivity
这一行。
public class MainActivity extends AppCompatActivity
implements OnFragmentInteractionListener
此外,如果使用支持库,那么AppCompatActivity是推荐的扩展类(扩展了FragmentActivity本身)
换句话说,
Random
然后,如果您从中获得错误,那么您可能在该活动的清单中缺少AppCompat主题
答案 1 :(得分:1)
在build.gradle中添加编译'com.android.support:appcompat-v7:24:2.0'
AppCompatActivity继承自FragmentActivity,因此如果您需要处理Fragments(可以通过Fragment Manager)。 AppCompatActivity适用于任何API,不仅仅是16+(谁说过?)。您可以通过在Gradle文件中添加编译'com.android.support:appcompat-v7:24:2.0'来使用它。我在API 10中使用它并且它完美无缺。
创建一个扩展AppCompatActivity的类;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class LoginActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
RegistrationFragment registrationFragment = new RegistrationFragment ();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragmentContainer, RegistrationFragment).addToBackStack(null).commitAllowingStateLoss();
}
}
RegistrationFragment是应扩展android.support.v4.app.Fragment的片段名称;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class RegistrationFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View parentView = inflater.inflate(R.layout.fragment_registration, container, false);
return parentView;
}
}