viewpager中有三个Fragment ,错误信息如下 Activity与一个fragment的代码如下,求帮忙解答
MainActivity代码如下
package com.aeron.mangaapp;
import java.util.ArrayList;
import com.aeron.fragments.AboutFragment;
import com.aeron.fragments.HomeFragment;
import com.aeron.fragments.HotFragment;
import com.aeron.myviewpager.MyViewPager;
import com.viewpagerindicator.TabPageIndicator;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends FragmentActivity {
//声明viewpager对象以及tabpageIndicator对象
private MyViewPager viewpager;
private TabPageIndicator indicator;
//声明装载Fragment的容器,以及Tab的String容器
ArrayList<Fragment> fragmentList;
private static String[] contents = new String[]{"首页","热门","关于"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//add the fragment
HomeFragment homeFragment = new HomeFragment();
HotFragment hotFragment = new HotFragment();
AboutFragment aboutFragment = new AboutFragment();
fragmentList = new ArrayList<Fragment>();
fragmentList.add(homeFragment);
fragmentList.add(hotFragment);
fragmentList.add(aboutFragment);
//初始化
viewpager = (MyViewPager)findViewById(R.id.fragmentviewpager);
indicator = (TabPageIndicator)findViewById(R.id.tabindicator);
viewpager.setAdapter(new CustomPageAdapter(getSupportFragmentManager()));
indicator.setViewPager(viewpager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//viewpager适配器
class CustomPageAdapter extends FragmentPagerAdapter{
public CustomPageAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return fragmentList.get(arg0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return fragmentList.size();
}
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return contents[position];
}
}
}
HomeFragment代码如下:
package com.aeron.fragments;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
public class HomeFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
//返回视图
TextView textview = new TextView(getActivity());
textview.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
textview.setText("Home");
textview.setGravity(Gravity.CENTER);
return textview;
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
}
}
------解决思路----------------------
看信息,只知道是v4里的那个方法报空指针......代码中实现看不出哪的问题,再debug一下看看吧.
------解决思路----------------------