0

FragmentPageAdapter から TextView の色を変更しようとしています。onHomeScreen または onSettings / onFriends 画面を呼び出すと、NullPointerException が発生します。これはテキストビューに対処する間違った方法ですか? それらは mainmenu.xml で宣言されており、onCreate メソッドで色やテキストを変更しても例外は発生しません。

私の MainMenu.java クラス。

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>

そして最後に私のLOGCATエラー。

    06-19 00:52:51.346: E/AndroidRuntime(21457): FATAL EXCEPTION: main
    06-19 00:52:51.346: E/AndroidRuntime(21457): java.lang.RuntimeException: Unable to                 start activity ComponentInfo{com.nordoid.tracephone/com.nordoid.tracephone.MainMenu}:    java.lang.NullPointerException
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at android.app.ActivityThread.access$600(ActivityThread.java:138)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at android.os.Handler.dispatchMessage(Handler.java:99)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at android.os.Looper.loop(Looper.java:213)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at android.app.ActivityThread.main(ActivityThread.java:4788)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at java.lang.reflect.Method.invokeNative(Native Method)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at java.lang.reflect.Method.invoke(Method.java:511)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at dalvik.system.NativeStart.main(Native Method)
    06-19 00:52:51.346: E/AndroidRuntime(21457): Caused by: java.lang.NullPointerException
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at com.nordoid.tracephone.MainMenu.onHomeScreen(MainMenu.java:116)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at com.nordoid.tracephone.MainMenu.onPageSelected(MainMenu.java:103)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:541)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:507)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:488)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at com.nordoid.tracephone.MainMenu.onCreate(MainMenu.java:41)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at android.app.Activity.performCreate(Activity.java:5008)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
    06-19 00:52:51.346: E/AndroidRuntime(21457):    ... 11 more
4

1 に答える 1