-1
我正在關注一些教程,無法在getInstance中解決此錯誤。請幫助無法解析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文件。在教程中,這工作正常,當我嘗試它時,它給了我錯誤。
爲什麼在公共靜態MyFragment.getInstance()中有一個點? – GPuschka
我也沒有明白這一點。 https://www.youtube.com/watch?v=Fl0xMuo10yA –
這裏是教程的鏈接 –