选项卡的PageAdapter和片段活动出错

时间:2013-09-26 22:26:54

标签: android android-fragments

我正在为我的dashboard.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"
android:background="#ffffff">

<TextView android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="@string/WELCOME"
          android:textSize="40dip"
          android:gravity="center"
          android:layout_marginTop="20dip"/>

   <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/emailTextView"/>

<Button android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/Logout_Me"
    android:textSize="20dip"
    android:textColor="#21dbd4"
    android:textStyle="bold"
    android:id="@+id/btnLogout"
    android:layout_marginTop="80dip"
    android:background="@null"/>

<RelativeLayout 
android:layout_width="match_parent"
android:layout_height="match_parent" >

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>

</RelativeLayout>

但是我经常在这两个java文件中出错: TabsViewPagerFragmentActivity.java

package com.example.loginandregistration;

IMPORTS

public class TabsViewPagerFragmentActivity extends FragmentActivity {

// Declare Variables
ActionBar mActionBar;
ViewPager mPager;
Tab tab;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from dashboard.xml
    setContentView(R.layout.dashboard);

    // Activate Navigation Mode Tabs
    mActionBar = getActionBar();
    mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Locate ViewPager in activity_main.xml
    mPager = (ViewPager) findViewById(R.id.pager);

    // Activate Fragment Manager
    FragmentManager fm = getFragmentManager();

    // Capture ViewPager page swipes
    ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            super.onPageSelected(position);
            // Find the ViewPager Position
            mActionBar.setSelectedNavigationItem(position);
        }
    };

    mPager.setOnPageChangeListener(ViewPagerListener);
    // Locate the adapter class called ViewPagerAdapter.java
    MyPageAdapter viewpageradapter = new MyPageAdapter(fm);
    // Set the View Pager Adapter into ViewPager
    mPager.setAdapter(viewpageradapter);

    // Capture tab button clicks
    ActionBar.TabListener tabListener = new ActionBar.TabListener() {

         @Override
         public void onTabSelected(Tab tab, FragmentTransaction ft) {
             // Pass the position on tab click to ViewPager
             mPager.setCurrentItem(tab.getPosition());
         }

         @Override
         public void onTabUnselected(Tab tab, FragmentTransaction ft) {
             // TODO Auto-generated method stub
         }

         @Override
         public void onTabReselected(Tab tab, FragmentTransaction ft) {
             // TODO Auto-generated method stub
         }
     };



    // Create first Tab
    tab = mActionBar.newTab().setText("Tab1").setTabListener(tabListener);
    mActionBar.addTab(tab);

    // Create second Tab
    tab = mActionBar.newTab().setText("Tab2").setTabListener(tabListener);
    mActionBar.addTab(tab);

    // Create third Tab
    tab = mActionBar.newTab().setText("Tab3").setTabListener(tabListener);
    mActionBar.addTab(tab);

}

}

(第46行:

MyPageAdapter viewpageradapter = new MyPageAdapter(fm);

错误:

The constructor MyPageAdapter(FragmentManager) is undefined TabsViewPagerFragmentActivity.java  /LoginAndRegistration/src/com/example/loginandregistration  line 46 Java Problem

但每当我添加一个构造函数时,我会得到另一个错误,这会产生一个连续的错误循环)

和MyPageAdapter:

package com.example.loginandregistration;

import android.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

public class MyPageAdapter extends FragmentPagerAdapter {

// Declare the number of ViewPager pages
final int PAGE_COUNT = 3;

public MyPageAdapter(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int arg0) {
    switch (arg0) {

    // Open FragmentTab1.java
    case 0:
        FragmentTab1 fragmenttab1 = new FragmentTab1();
        return fragmenttab1;

    // Open FragmentTab2.java
    case 1:
        FragmentTab2 fragmenttab2 = new FragmentTab2();
        return fragmenttab2;

    // Open FragmentTab3.java
    case 2:
        FragmentTab3 fragmenttab3 = new FragmentTab3();
        return fragmenttab3;
    }
    return null;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return PAGE_COUNT;

}

}

(行上的错误: 18

public Fragment getItem(int arg0) {

23

FragmentTab1 fragmenttab1 = new FragmentTab1();

28

FragmentTab2 fragmenttab2 = new FragmentTab2();

33

 FragmentTab3 fragmenttab3 = new FragmentTab3();

使用第18行,它表示返回类型无效,并表示将其更改为“(Fragment)” 使用第23,28,33行,它表示FragmentTabs无法解析为某种类型但是当我把我想要的类作为FragmentTabs时(如下所示:

        // Open FragmentTab1.java
    case 0:
        CreatePic createpic = new CreatePic();
        return createpic;

) 我得到错误,说“CreatePic”被识别并且返回错误(当我点击quickfix时,它会创建一个错误循环)

我该如何解决这个问题?

ADDED CREATEPIC.JAVA(上):

package com.example.loginandregistration;


IMPORTS
public class CreatePic extends View {

//drawing path
private Path drawPath;
//drawing and canvas paint
private Paint drawPaint, canvasPaint;
//initial color
private int paintColor = 0xFF660000, paintAlpha = 255;
//canvas
private Canvas drawCanvas;
//canvas bitmap
private Bitmap canvasBitmap;
//brush sizes
private float brushSize, lastBrushSize;
//erase flag
private boolean erase=false;

public CreatePic(Context context, AttributeSet attrs){
    super(context, attrs);
    setupDrawing();
}

//setup drawing
private void setupDrawing(){

    //prepare for drawing and setup paint stroke properties
    brushSize = getResources().getInteger(R.integer.medium_size);
    lastBrushSize = brushSize;
    drawPath = new Path();
    drawPaint = new Paint();
    drawPaint.setColor(paintColor);
    drawPaint.setAntiAlias(true);
    drawPaint.setStrokeWidth(brushSize);
    drawPaint.setStyle(Paint.Style.STROKE);
    drawPaint.setStrokeJoin(Paint.Join.ROUND);
    drawPaint.setStrokeCap(Paint.Cap.ROUND);
    canvasPaint = new Paint(Paint.DITHER_FLAG);
}

//size assigned to view
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    drawCanvas = new Canvas(canvasBitmap);
}

//draw the view - will be called after touch event
@Override
protected void onDraw(Canvas canvas) {
    canvas.drawBitmap(canvasBitmap, 0, 0, canvasPaint);
    canvas.drawPath(drawPath, drawPaint);
}

1 个答案:

答案 0 :(得分:1)

我假设你已经修正了所有的进口。您必须为CreatePic创建一个片段。转到文件&gt;新&gt;将它命名为CreatePic.java

public class CreatePic extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Get the view from createpic.xml
        View view = inflater.inflate(R.layout.createpic , container, false);
        return view;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        setUserVisibleHint(true);
    }

}