我在使用FragmentStatePageAdapter的选项卡中有一个ViewPager。活动从远程服务器中的数据库中检索imageUrls列表,然后在viewPager中显示这些图像。每次我运行它,它检索列表罚款,但它给我一个“IllegalStateException指定的孩子已经有一个父”这发生在行“mPageAdapter = new ImagePagerAdapter(getSupportFragmentManager(),list.length());”我不明白为什么。下面是我的所有代码,包括适配器,Fragment和xml以及log cat谢谢。请帮帮我。
XML
<TabHost
android:id="@+id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="@+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button_grid"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
<Button
android:id="@+id/button_refresh"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="@+id/myviewpager1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="9" >
</android.support.v4.view.ViewPager>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
片段XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
使用pageAdapter的活动
package com.example.nightowl;
import java.io.IOException;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.Toast;
import com.squareup.picasso.Picasso;
public class Feed extends FragmentActivity {
TabHost th;
public static ArrayList<String> imageUrl = new ArrayList<String>();
public int width, height;
public JSONArray list;
ImageView image;
ViewPager mPager;
public ImagePagerAdapter mPageAdapter;
public final String IMAGE_URL = "http://10.0.0.104:80/webservice/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_feed);
//Method to set up the tabs
setupTabs();
getSizes();
updateList();
mPager = (ViewPager) findViewById(R.id.myviewpager1);
mPager.setOffscreenPageLimit(1);
}
//Called from the fragment which fetches image
public void loadBitmap(String image, ImageView imageView) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.black);
/*try {
bmp = Picasso.with(Feed.this).load(IMAGE_URL+image).resize(width, width).get();
} catch (IOException e) {
Toast.makeText(Feed.this, "CONNECTION ERROR", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
RoundedCornersDrawable drawable = new RoundedCornersDrawable(getResources(), bmp);
imageView.setImageDrawable(drawable);
*/
Log.d("Currnt image", image);
Picasso.with(Feed.this).load("http://10.0.0.104:80/webservice/" + image).resize(width,
width).into(imageView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.feed, menu);
return true;
}
//SET UP THE TWO TABS 'FEED' AND 'LOCATIONS'
public void setupTabs() {
th = (TabHost) findViewById(R.id.tabhost);
th.setup();
TabSpec specs = th.newTabSpec("tag1");
specs.setContent(R.id.tab1);
specs.setIndicator("Feed");
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.tab2);
specs.setIndicator("Locations");
th.addTab(specs);
}
//Update ArrayList of the image names
public void updateList() {
imageUrl.clear();
new getImageUrl().execute();
}
class getImageUrl extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... arg0) {
JSONParser parser = new JSONParser();
JSONObject json =
parser.getJSONFromUrl("http://10.0.0.104:80/webservice/updateimages.php");
try {
list = json.getJSONArray("posts");
for(int i = 0; i < list.length(); i++) {
JSONObject c = list.getJSONObject(i);
String imgSrc = c.getString("source");
imageUrl.add(imgSrc);
}
Log.d("Got list", imageUrl.get(0));
} catch(JSONException je) {
je.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
updateAdapter();
}
}
public void updateAdapter() {
mPageAdapter = new ImagePagerAdapter(getSupportFragmentManager(), list.length());
mPager.setAdapter(mPageAdapter);
}
//GET SIZES OF SCREEN
public void getSizes(){
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
}
public static class ImagePagerAdapter extends FragmentStatePagerAdapter {
int mSize;
public ImagePagerAdapter(FragmentManager fm, int size) {
super(fm);
mSize = size;
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
Log.d("Starting imageDetailFragment", "");
return ImageDetailFragment.newInstance(arg0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mSize;
}
}
}
片段类
package com.example.nightowl;
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.ImageView;
public class ImageDetailFragment extends Fragment {
int mImageNum;
ImageView imageView;
static ImageDetailFragment newInstance(int imageNum) {
final ImageDetailFragment f = new ImageDetailFragment();
Bundle args = new Bundle();
args.putInt("imageNum", imageNum);
f.setArguments(args);
return f;
}
public ImageDetailFragment(){}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) mImageNum = getArguments().getInt("imageNum");
else mImageNum = -1;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.image_detail_fragment, container);
imageView = (ImageView) v.findViewById(R.id.imageView1);
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if(Feed.class.isInstance(getActivity())) {
final String image = Feed.imageUrl.get(mImageNum);
((Feed) getActivity()).loadBitmap(image, imageView);
}
};
}
logcat的
09-22 22:17:10.880: W/dalvikvm(7470): threadid=1: thread exiting with uncaught exception
(group=0x41bcb700)
09-22 22:17:10.885: E/AndroidRuntime(7470): FATAL EXCEPTION: main
09-22 22:17:10.885: E/AndroidRuntime(7470): java.lang.IllegalStateException: The specified child
already has a parent. You must call removeView() on the child's parent first.
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.view.ViewGroup.addViewInner(ViewGroup.java:3739)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.view.ViewGroup.addView(ViewGroup.java:3610)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.view.ViewGroup.addView(ViewGroup.java:3555)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.view.ViewGroup.addView(ViewGroup.java:3531)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.support.v4.app.NoSaveStateFrameLayout.wrap(NoSaveStateFrameLayout.java:40)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:915)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.support.v4.view.ViewPager.populate(ViewPager.java:1064)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.support.v4.view.ViewPager.populate(ViewPager.java:911)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.support.v4.view.ViewPager.setAdapter(ViewPager.java:440)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
com.example.nightowl.Feed.updateAdapter(Feed.java:145)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
com.example.nightowl.Feed$getImageUrl.onPostExecute(Feed.java:139)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
com.example.nightowl.Feed$getImageUrl.onPostExecute(Feed.java:1)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.os.AsyncTask.finish(AsyncTask.java:631)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.os.AsyncTask.access$600(AsyncTask.java:177)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.os.Handler.dispatchMessage(Handler.java:99)
09-22 22:17:10.885: E/AndroidRuntime(7470): at android.os.Looper.loop(Looper.java:176)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
android.app.ActivityThread.main(ActivityThread.java:5419)
09-22 22:17:10.885: E/AndroidRuntime(7470): at java.lang.reflect.Method.invokeNative(Native
Method)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
java.lang.reflect.Method.invoke(Method.java:525)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
09-22 22:17:10.885: E/AndroidRuntime(7470): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
09-22 22:17:10.885: E/AndroidRuntime(7470): at dalvik.system.NativeStart.main(Native Method)
我正在使用JsonParser和Picasso库来更新imageList并获取图像,一切正常。
答案 0 :(得分:0)
如果有人仍然感兴趣,我发现了问题。在片段的OnCreateView()方法中,当你给视图充气时,你需要添加“false”作为最后一个参数......
final View v = inflater.inflate(R.layout.image_detail_fragment, container, false);