0

我得到一个资源未找到异常,我不确定为什么。我在想这个错误的方式吗?我试图将 setTextColor 标签移动到不同的位置,但它仍然无法正常工作。我让它正常工作的唯一方法是在 onCreate 方法中。所以应用程序能够在 onCreate 中找到权限 ID,但不能在其他方法中找到?有人知道为什么吗?

ViewPager 适配器:

 package com.nordoid.tracephone;
 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);

    home = (TextView) findViewById(R.id.mmenu_home);
    settings = (TextView) findViewById(R.id.mmenu_settings);
    friends = (TextView) findViewById(R.id.mmenu_friends);

    mAdapter = new MyAdapter(getSupportFragmentManager());
    mPager = (ViewPager) findViewById(R.id.mainmenu_pager);
    mPager.setAdapter(mAdapter);
    mPager.setOnPageChangeListener(this);
    mPager.setCurrentItem(1);

    // 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);
}
}

ViewPagrAdapter 布局:

<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>

</RelativeLayout>

以及 LogCAT 错误输出。

06-23 18:50:47.685: E/InputEventReceiver(9936): Exception dispatching input event.
06-23 18:50:47.685: E/MessageQueue-JNI(9936): Exception in MessageQueue callback:     handleReceiveCallback
06-23 18:50:47.695: E/MessageQueue-JNI(9936): android.content.res.Resources$NotFoundException: String resource ID #0xff888888
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.content.res.Resources.getText(Resources.java:234)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.widget.TextView.setText(TextView.java:3672)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at com.nordoid.tracephone.MainMenu.onFriendsScreen(MainMenu.java:150)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at com.nordoid.tracephone.MainMenu.onPageSelected(MainMenu.java:124)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:565)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:549)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2004)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.view.View.dispatchTouchEvent(View.java:7190)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2274)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2009)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at     android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1931)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1385)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1879)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.view.View.dispatchPointerEvent(View.java:7370)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3205)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3150)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4187)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4166)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4258)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.os.MessageQueue.nativePollOnce(Native Method)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.os.MessageQueue.next(MessageQueue.java:125)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.os.Looper.loop(Looper.java:197)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at android.app.ActivityThread.main(ActivityThread.java:4788)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at java.lang.reflect.Method.invokeNative(Native Method)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at java.lang.reflect.Method.invoke(Method.java:511)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-23 18:50:47.695: E/MessageQueue-JNI(9936):   at dalvik.system.NativeStart.main(Native Method)
06-23 18:50:47.705: W/dalvikvm(9936): threadid=1: thread exiting with uncaught exception (group=0x41837450)
06-23 18:50:47.705: E/AndroidRuntime(9936): FATAL EXCEPTION: main
06-23 18:50:47.705: E/AndroidRuntime(9936): android.content.res.Resources$NotFoundException: String resource ID #0xff888888
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.content.res.Resources.getText(Resources.java:234)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.widget.TextView.setText(TextView.java:3672)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at com.nordoid.tracephone.MainMenu.onFriendsScreen(MainMenu.java:150)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at com.nordoid.tracephone.MainMenu.onPageSelected(MainMenu.java:124)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:565)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:549)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2004)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.view.View.dispatchTouchEvent(View.java:7190)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2274)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2009)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2280)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2023)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1931)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1385)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1879)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.view.View.dispatchPointerEvent(View.java:7370)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3205)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3150)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4187)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4166)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4258)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.os.MessageQueue.nativePollOnce(Native Method)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.os.MessageQueue.next(MessageQueue.java:125)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.os.Looper.loop(Looper.java:197)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at android.app.ActivityThread.main(ActivityThread.java:4788)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at java.lang.reflect.Method.invokeNative(Native Method)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at java.lang.reflect.Method.invoke(Method.java:511)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-23 18:50:47.705: E/AndroidRuntime(9936):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

这个

  home.setText(Color.GRAY); //expecting  a resource id which is an int
  // the resource does not exist. hence you get ResourceNotFoundException 

例子:

考虑到您有如下字符串资源

   <string name="app_name">MyAppName</string> 

您可以执行以下操作

  home.setText(R.string.app_name);
  // the resource referred exists in string.xml.

http://developer.android.com/reference/android/widget/TextView.html

检查文档以获取更多信息。

public final void setText (int resid)
//expects a resource id which is an integer. 

不确定你在做什么。如果要显示颜色值,可以执行以下操作。

 home.setText(String.valueOf(Color.GRAY));
于 2013-06-23T17:05:01.063 回答