这是我在MainActivity类中的代码
package com.example.movieapp;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
ListView main_list;
ArrayAdapter<Cinema> adapter;
ArrayList<Cinema> ItemList = new ArrayList<Cinema>();
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.v("Check", "1");
View rootView = inflater.inflate(R.layout.fragment_main, container,
ItemList.addAll(initialization()));
ListView main_list = (ListView) rootView.findViewById(R.id.mainlist);
adapter = new ListAdapater(rootView.getContext(), ItemList);
main_list.setAdapter(adapter);
return rootView;
}
public ArrayList<Cinema> initialization () {
ArrayList<Cinema> tempItemList = new ArrayList<Cinema>();
ArrayList<Movie> movielistone = new ArrayList<Movie>();
Movie movieone = new Movie(101, "MovieOne", "null", 5, "nULL");
Movie movietwo = new Movie(102, "Movietwo", "null", 3, "nuLL");
movielistone.add(movieone);
movielistone.add(movietwo);
Cinema cinemaone = new Cinema(01, "test", "ygn", 55555, movielistone, 3);
Cinema cinematwo = new Cinema(02, "test2", "ygn", 554555, movielistone, 3);
tempItemList.add(cinemaone);
tempItemList.add(cinematwo);
return tempItemList;
}
}
}
说实话,我知道为什么会出现这个错误。这是我在模拟器上运行它时得到的错误日志
05-22 06:23:39.640: E/AndroidRuntime(913): FATAL EXCEPTION: main
05-22 06:23:39.640: E/AndroidRuntime(913): Process: com.example.movieapp, PID: 913
05-22 06:23:39.640: E/AndroidRuntime(913): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.movieapp/com.example.movieapp.MainActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-22 06:23:39.640: E/AndroidRuntime(913): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-22 06:23:39.640: E/AndroidRuntime(913): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-22 06:23:39.640: E/AndroidRuntime(913): at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-22 06:23:39.640: E/AndroidRuntime(913): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-22 06:23:39.640: E/AndroidRuntime(913): at android.os.Handler.dispatchMessage(Handler.java:102)
05-22 06:23:39.640: E/AndroidRuntime(913): at android.os.Looper.loop(Looper.java:136)
05-22 06:23:39.640: E/AndroidRuntime(913): at android.app.ActivityThread.main(ActivityThread.java:5017)
05-22 06:23:39.640: E/AndroidRuntime(913): at java.lang.reflect.Method.invokeNative(Native Method)
05-22 06:23:39.640: E/AndroidRuntime(913): at java.lang.reflect.Method.invoke(Method.java:515)
05-22 06:23:39.640: E/AndroidRuntime(913): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-22 06:23:39.640: E/AndroidRuntime(913): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-22 06:23:39.640: E/AndroidRuntime(913): at dalvik.system.NativeStart.main(Native Method)
05-22 06:23:39.640: E/AndroidRuntime(913): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
05-22 06:23:39.640: E/AndroidRuntime(913): at android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
05-22 06:23:39.640: E/AndroidRuntime(913): at android.view.ViewGroup.addView(ViewGroup.java:3415)
05-22 06:23:39.640: E/AndroidRuntime(913): at android.view.ViewGroup.addView(ViewGroup.java:3360)
05-22 06:23:39.640: E/AndroidRuntime(913): at android.view.ViewGroup.addView(ViewGroup.java:3336)
05-22 06:23:39.640: E/AndroidRuntime(913): at android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
05-22 06:23:39.640: E/AndroidRuntime(913): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
05-22 06:23:39.640: E/AndroidRuntime(913): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
05-22 06:23:39.640: E/AndroidRuntime(913): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-22 06:23:39.640: E/AndroidRuntime(913): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
05-22 06:23:39.640: E/AndroidRuntime(913): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
05-22 06:23:39.640: E/AndroidRuntime(913): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
05-22 06:23:39.640: E/AndroidRuntime(913): at android.app.Activity.performStart(Activity.java:5241)
05-22 06:23:39.640: E/AndroidRuntime(913): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
是因为我使用ViewFlow库吗?感谢您抽出宝贵时间阅读本文。
答案 0 :(得分:2)
View rootView = inflater.inflate(R.layout.fragment_main, container,
这条线不完整。您是否将第三个参数设置为true
?如果是这样,那就是你的问题。
http://developer.android.com/guide/components/fragments.html#UI