Android 已选择onPageSelected上的ViewPager和片段异常。
我正在尝试从FragmentPageAdapter更改文本视图的颜色。当我在主屏幕或设置/朋友屏幕上调用时,我得到一个空点异常。这是解决文本视图的错误方法吗?它们在main menu.xml中被清除,在onCreate方法中更改颜色/文本时不会发生异常 我的MainMenu.java类Android 已选择onPageSelected上的ViewPager和片段异常。,android,android-fragments,android-viewpager,textview,Android,Android Fragments,Android Viewpager,Textview,我正在尝试从FragmentPageAdapter更改文本视图的颜色。当我在主屏幕或设置/朋友屏幕上调用时,我得到一个空点异常。这是解决文本视图的错误方法吗?它们在main menu.xml中被清除,在onCreate方法中更改颜色/文本时不会发生异常 我的MainMenu.java类 package com.nordoid.*; import android.graphics.Color; import android.os.Bundle; import android.support.v4.
package com.nordoid.*;
import android.graphics.Color;
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.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.widget.TextView;
public class MainMenu extends FragmentActivity implements OnPageChangeListener {
private MyAdapter mAdapter;
private ViewPager mPager;
// MainMenu bar
TextView home;
TextView settings;
TextView friends;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.mainmenu_pager);
mPager.setAdapter(mAdapter);
mPager.setOnPageChangeListener(this);
mPager.setCurrentItem(1);
home = (TextView) findViewById(R.id.mmenu_home);
settings = (TextView) findViewById(R.id.mmenu_settings);
friends = (TextView) findViewById(R.id.mmenu_friends);
// init starting position color.
//home.setTextColor(Color.WHITE);
home.setText("HOME");
settings.setText("SETTINGS");
friends.setText("FRIENDS")
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 3;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new Mfragment_settings();
case 1:
return new Mfragment_main();
case 2:
return new Mfragment_friends();
default:
return null;
}
}
}
public void onPageScrollStateChanged(int arg0) {
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageSelected(int arg0) {
switch (arg0) {
case 0:
onSettingsScreen();
break;
case 1:
onHomeScreen();
break;
case 2:
onFriendsScreen();
break;
}
}
public void onHomeScreen () {
home.setTextColor(Color.WHITE);
System.out.println("2");
friends.setTextColor(Color.GRAY);
settings.setTextColor(Color.GRAY);
}
public void onSettingsScreen () {
settings.setTextColor(Color.WHITE);
friends.setTextColor(Color.GRAY);
home.setTextColor(Color.GRAY);
System.out.println("PAGE 1");
}
public void onFriendsScreen () {
friends.setTextColor(Color.WHITE);
System.out.println("3");
home.setText(Color.GRAY);
settings.setTextColor(Color.GRAY);
}
}
和我的mainmenu.xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/mfrag_home" >
<android.support.v4.view.ViewPager
android:id="@+id/mainmenu_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="27dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginTop="45dp" >
<TextView
android:id="@+id/mmenu_settings"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="15dp"
android:paddingTop="2dp"
android:text="TextView"
android:textColor="@color/White" />
<TextView
android:id="@+id/mmenu_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="45dp"
android:paddingTop="2dp"
android:text="TextView"
android:textColor="@color/White" />
<TextView
android:id="@+id/mmenu_friends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="49dp"
android:paddingTop="2dp"
android:text="TextView"
android:textColor="@color/White" />
</LinearLayout>
我认为您有一个例外,因为当调用mPager.setCurrentItem(1)时,textview home friends&settings为null(初始化发生在之后) 移动:
home = (TextView) findViewById(R.id.mmenu_home);
settings = (TextView) findViewById(R.id.mmenu_settings);
friends = (TextView) findViewById(R.id.mmenu_friends);
之前:
mPager = (ViewPager) findViewById(R.id.mainmenu_pager);
应该解决NullPointerException
mPager = (ViewPager) findViewById(R.id.mainmenu_pager);