在pageAdapter中调用片段无法解析newInstance方法

时间:2015-09-29 19:23:31

标签: android-fragments fragment adapter instance

在此代码中,错误是它无法解析PageAdapter类中的newInstance方法,并且它表示永远不会在Fragment.java中使用newInstance。我该如何解决这个问题?

我希望有一个包含viewpager和fragment的页面。滑动手指时,它会从字符串

更改mt文本视图的文本

ListActivity.java

.state("main", {
            url: "/main",
            templateUrl: "templates/Ders",
            controller: "DersController as DersC"
       }).state("main.child1", {
            url: "/child1",
            templateUrl: "templates/Ders/child1"
       }).state("main.child2", {
            url: "/child2",
            templateUrl: "templates/Ders/child2"
        })

这是我的PageAdapter.java

package com.example.farjad.mymoneyapp.ListViewPages.Page1;

import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.example.farjad.mymoneyapp.Adaptor.PageAdapter;
import com.example.farjad.mymoneyapp.R;

public class ListActivity1_P1 extends FragmentActivity {
    private ViewPager viewPager;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_activity1__p1);
        viewPager = (ViewPager) findViewById(R.id.curl);
        viewPager.setAdapter(new PageAdapter(getSupportFragmentManager()));



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_list_activity1__p1, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Fragment.java

     package com.example.farjad.mymoneyapp.Adaptor;

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

import com.example.farjad.mymoneyapp.R;

/**
 * Created by Farjad on 9/27/2015.
 */
public class PageAdapter extends FragmentPagerAdapter {


    private  String[] string;

    public PageAdapter(FragmentManager fm, String [] stringstodisplay) {
        super(fm);
        this.string = stringstodisplay;


    }

    public PageAdapter(FragmentManager supportFragmentManager) {
        super(supportFragmentManager);
    }

    @Override
    public android.support.v4.app.Fragment getItem(int position) {

        switch (position) {
            default:
            case 0:
                return Fragment.newInstance(0, android.app.Fragment.class.getSigners());

            case 1:
                return new Fragment.instantiate(0, getString( R.string.s1p1));;

            case 2:
                return new Fragment..instantiate(0, getString( R.string.s1p2));;;


        }


    }

    @Override
    public int getCount() {
        return 5;
    }
}

1 个答案:

答案 0 :(得分:1)

这是因为你打电话给你的Fragment班级 - Fragment,所以在PageAdapter它导入了支持Fragment类,而不是你扩展的Fragment类。我会将您的Fragment类重命名为其他任何类(例如,MyFragment)并让IDE为您解析依赖项,但您也可以更改import语句中的PageAdapter语句} class来导入你的Fragment类。