无法解析android

时间:2016-06-14 07:07:51

标签: android

我正在学习一些教程,但无法在getInstance中解决此错误。请帮忙

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.s.foodapp.SlidingTab.SlidingTabLayout;

public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private ViewPager mPager;
private SlidingTabLayout mTabs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    toolbar= (Toolbar)findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    mPager=(ViewPager)findViewById(R.id.pager);
    mTabs=(SlidingTabLayout)findViewById(R.id.tab);
    mPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
    mTabs.setViewPager(mPager);

}
class MyPagerAdapter extends FragmentPagerAdapter{
    String[] tabs;
    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
        tabs= getResources().getStringArray(R.array.tabs);
    }

    @Override
    public Fragment getItem(int position) {
        MyFragment myFragment= MyFragment.getInstance(position);// cannot resolve get instance
        return myFragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return tabs[position];
    }

    @Override
    public int getCount() {
        return 3;
    }
}
public  static class MyFragment extends Fragment{
    private TextView textView;
    public static MyFragment.getInstance(int position){// cannot resolve get instance
        MyFragment myFragment= new MyFragment();
        Bundle args=new Bundle();
        args.putInt("position",position);
        myFragment.setArguments(args);
        return myFragment;
    }
    @Override
    public  View onCreateView(LayoutInflater layoutInflater, @Nullable ViewGroup container,@Nullable Bundle savedInstanceState){
        View view= layoutInflater.inflate(R.layout.fragment_my,container,false);
        textView= (TextView)view.findViewById(R.id.position);
        Bundle bundle=getArguments();
        if(bundle!=null){
            textView.setText("The page selected is "+bundle.getInt("position"));
        }
        return view;
    }


}

}

这是MainActivity文件。在教程中,这工作正常,当我尝试它时它给了我错误。

2 个答案:

答案 0 :(得分:1)

只需替换此方法即可。 在方法声明中,不要使用"。"

public static MyFragment getInstance(int position){// cannot resolve get instance
        MyFragment myFragment= new MyFragment();
        Bundle args=new Bundle();
        args.putInt("position",position);
        myFragment.setArguments(args);
        return myFragment;
    }

答案 1 :(得分:0)

不要使用'。'。您正在尝试编写一个返回片段实例的静态方法。所以请这样使用。

public static MyFragment getInstance(int position)
{
        MyFragment myFragment= new MyFragment();
        Bundle args=new Bundle();
        args.putInt("position",position);
        myFragment.setArguments(args);
        return myFragment;
}