错误地方
代码
package com.example.fiz.myapplication;
import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.util.ArrayList;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
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.view.Menu;
import android.view.MenuItem;
import android.widget.LinearLayout;
import android.support.v7.widget.Toolbar;
import android.widget.TextView;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ViewPager mPager;
private ArrayList<Fragment> fragmentsList;
private LinearLayout ll_top;
//LITview
//定义一个String数组用来显示ListView的内容private ListView lv;/** Called when the activity is first created. */
private static final String[] strs=new String[]{"first", "second", "third", "fourth", "fifth"};
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("demo");
toolbar.setSubtitle("1234");
toolbar.setLogo(R.drawable.ic_launcher);
setSupportActionBar(toolbar);
//
initView();
}
private void initView() {
ll_top = (LinearLayout) findViewById(R.id.ll_top);
mPager = (ViewPager) findViewById(R.id.vPager);
fragmentsList = new ArrayList<Fragment>();
Fragment activityfragment = TestFragment.newInstance("界面一");
Fragment groupFragment = TestFragment.newInstance("界面2");
Fragment friendsFragment = TestFragment.newInstance("界面3");
Fragment chatFragment = TestFragment.newInstance("界面4");
Fragment about = About.about("1234");
//实例化
Fragment homepage = new HomePage("首页");
fragmentsList.add(homepage);/**首页**/
fragmentsList.add(groupFragment);
fragmentsList.add(friendsFragment);
fragmentsList.add(chatFragment);
mPager.setAdapter(new MyFragmentPagerAdapter(
getSupportFragmentManager(), fragmentsList));
mPager.setCurrentItem(0);
BottomTabBar navigationBar = new BottomTabBar(this);
ll_top.setBackgroundColor(Color.parseColor("#F8F8FF"));
navigationBar.attachToParent(ll_top, new String[]{"首页", "消息", "发现",
"我"}, mPager);
}
@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_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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment> fragmentsList;
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
public MyFragmentPagerAdapter(FragmentManager fm,
ArrayList<Fragment> fragments) {
super(fm);
this.fragmentsList = fragments;
}
@Override
public int getCount() {
return fragmentsList.size();
}
@Override
public Fragment getItem(int arg0) {
return fragmentsList.get(arg0);
}
@Override
public int getItemPosition(Object object) {
return super.getItemPosition(object);
}
}
@SuppressLint("ValidFragment")
public class HomePage extends Fragment {
private String textString;
//复制添加
public HomePage(String textString) {
this.textString = textString;
}
public HomePage homePage(String textString) {
HomePage myabout = new HomePage(textString);
return myabout;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
lv = (ListView)findViewById(R.id.lv);
lv.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strs));
View view = inflater.inflate(R.layout.home_page, container, false);
return view;
}
}
}
上下文对象用的不对,fragment中adapter的this得用getActivity()替代啊。
ArrayAdapter的构造方法的第一参数是要一个上下文类型的,你传的this是HomePage,类型不符合无法解析