在工作线程中运行时,查看Pager在Fragment中不起作用

时间:2014-01-03 06:18:49

标签: android-fragments android-viewpager

在Fragment中,我正在尝试使用工作线程在视图寻呼机中加载数据。当它处于活动状态时,相同的代码正在工作,但片段中的代码正在提供异常。例外情况是说“doInBackground”导致了一些问题。但在“doInBackground”中,我只是将数据分配给某个变量。

HTML5Test.java
==============

package com.example.asynctaskfra;



import java.lang.ref.WeakReference;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
public class HTML5Test extends Fragment {




    String[] displayDatainHTML5 = { "tstessssssssssssssssssssssssssssssssssssssssssfeugiat, vitae consectetur","Checl something Nulla dolor ligula, 123456677777777","13232","312321313","e2324234324"};
    private ImageButton ViewPageReportLeft;
    private ImageButton ViewPageReportRight;
    private ImageButton showCalendar;
    private ImageButton sharing;
    private TextView showDate;
    private Handler m_handler;
    int count =5;
    String Format = "MMMM dd, yyyy";
    android.support.v4.view.ViewPager viewPager;
    View convertView;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        convertView = inflater.inflate(R.layout.html5test, container, false);

        viewPager = (android.support.v4.view.ViewPager)convertView.findViewById(R.id.Content);
        ViewPageReportLeft = (ImageButton) convertView.findViewById(R.id.LeftArrow);
        ViewPageReportRight = (ImageButton) convertView.findViewById(R.id.RightArrow);
        showCalendar = (ImageButton) convertView.findViewById(R.id.Calendar);
        sharing = (ImageButton) convertView.findViewById(R.id.Sharing);


        initControls();


        return convertView; 

    }



    private void initControls() {




        new BackGroundTask(viewPager).execute();    
    }


    ProgressDialog waitdialog;
    public  class BackGroundTask extends AsyncTask<String, Void ,Void>{


        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
              super.onPreExecute();
              //waitdialog=ProgressDialog.show(HTML5Content.this, "", "please wait data is loading");
              Log.d("Post Execute", "Is excuted");


        }
            @Override
            protected Void doInBackground(String... params) {
                // TODO Auto-generated method stub

                count=displayDatainHTML5.length;
                return null;
            }
            @Override
                protected void onPostExecute(Void result) {
                    // TODO Auto-generated method stub

                    super.onPostExecute(result);
                            viewPager.setAdapter(new TextPagerAdapter(getActivity(),displayDatainHTML5,Color.GREEN));
                            m_handler = new Handler();
                            startRepeatingTask();
                            //Pageindicator(0);
                            waitdialog.dismiss();


                }
       }


    private void adsRotation() {

        int cur=viewPager.getCurrentItem();
        if(cur==count-1){
            cur=0;
            viewPager.setCurrentItem(cur);
        }
        else{
            cur=cur+1;
            viewPager.setCurrentItem(cur);
        }

    }

    Runnable m_statusChecker = new Runnable() {
        public void run() {
            adsRotation(); // this function can change value of m_interval.
            Log.d("Check" , "Whether it is running");
            m_handler.postDelayed(m_statusChecker, 6000);
        }
    };

    void startRepeatingTask() {
        m_statusChecker.run();
    }

    void stopRepeatingTask() {
        m_handler.removeCallbacks(m_statusChecker);
    }


}


html5test.xml
=============

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout

    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical" >



    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="0.15"
        android:orientation="horizontal"
        android:scaleType="fitXY" >

        <ImageView
            android:id="@+id/TopImage"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50dp"
            android:layout_marginTop="0dp"
            android:layout_weight="0.43"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:scaleType="fitXY" >

        <TextView
            android:id="@+id/Date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="0dp"
            android:src="@drawable/ic_launcher"
            android:text="ShowDate" />

        <ImageButton
            android:id="@+id/Sharing"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="110dp"
            android:layout_marginTop="0dp"
            android:layout_weight="0.44"
            android:src="@drawable/ic_launcher" />

        <ImageButton
            android:id="@+id/Calendar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="0dp"
            android:layout_weight="0.44"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:scaleType="fitXY" >

        <ImageView
            android:id="@+id/Blackline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="0dp"
            android:layout_weight="0.44"
            android:src="@drawable/blackline" />

    </LinearLayout>



    <LinearLayout
        android:id="@+id/OutputLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:scaleType="fitXY" >

        <android.support.v4.view.ViewPager
            android:id="@+id/Content"
            android:layout_width="wrap_content"
            android:layout_height="200dp"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="0dp"
            android:src="@drawable/ic_launcher"
             />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:scaleType="fitXY" >

        <ImageView
            android:id="@+id/Blackline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dp"
            android:layout_marginTop="0dp"
            android:layout_weight="0.44"
            android:src="@drawable/blacklinethin" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:scaleType="fitXY" >

        <ImageButton
            android:id="@+id/LeftArrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="0dp"
            android:layout_weight="0.44"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/Read"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="30dp"
            android:layout_marginTop="0dp"
            android:src="@drawable/ic_launcher"
            android:text="Read More Featured Stories" />

        <ImageButton
            android:id="@+id/RightArrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="0dp"
            android:layout_weight="0.44"
            android:src="@drawable/ic_launcher" />
    </LinearLayout>

    </LinearLayout>

</FrameLayout>

TextPagerAdapter.java
=====================

package com.example.asynctaskfra;
import java.util.ArrayList;
import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class TextPagerAdapter extends PagerAdapter {

    private LayoutInflater inflater;
    Context context;
    String [] displayData;
    int passColor;

    public TextPagerAdapter(Context context,String [] displayData,int passColor) {
        this.displayData=displayData;
        inflater = LayoutInflater.from(context);    
        this.context=context;
        this.passColor = passColor;

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((View) object);
    }

    @Override
    public void finishUpdate(View container) {
    }

    @Override
    public int getCount() {

        return displayData.length;
    }

    @Override
    public Object instantiateItem(ViewGroup view, final int position) {
        View imageLayout = inflater.inflate(R.layout.list_single, view, false);
        TextView title = (TextView) imageLayout.findViewById(R.id.displaytxt);
        title.setText(displayData[position]);
        title.setTextColor(passColor);
        ((ViewPager) view).addView(imageLayout, 0);
        return imageLayout;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(object);
    }

    @Override
    public void restoreState(Parcelable state, ClassLoader loader) {
    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View container) {
    }
}

list_single.xml
===============


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    >

        <TextView
            android:id="@+id/displaytxt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="30dp"
            android:gravity="center"
            android:textColor="#ffffff" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

嗨,请将PagerAdapter替换为FragmentPagerAdapter,它正常工作。