2013-12-20 5 views
0

У меня проблема с использованием findViewById. Он возвращает null. Я создаю игру, и у меня есть активность, которая является моим меню. Оттуда я создаю и запускаю свой SurfaceView. Но я не могу получить ссылку на FPS TextView с помощью findViewById.findViewById возвращает null

public class SplashScreen extends Activity { 

    . 
    . 
    . 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.splash_screen_layout); 

     . 
     . 
     . 

    } 

    private void prepareButtonListeners() { 
     this.mStartButton.setOnClickListener(new OnClickListener() {    
      @Override 
      public void onClick(View v) { 
       setContentView(R.layout.road_view_layout); 
      } 
     }); 
     . 
     . 
     . 


<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <br.com.roadrun.RoadView 
     android:id="@+id/road_view" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

    <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 

     <TextView 
     android:id="@+id/fps_text_view" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" /> 

    </RelativeLayout> 

</FrameLayout> 


public class RoadView extends SurfaceView implements SurfaceHolder.Callback { 

    private Context mContext = null; 
    private SurfaceHolder mSurfaceHolder = null; 
    private TextView mFPSTextView = null; 

    private RoadThread mThread = null; 

    public RoadView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     this.mContext = context; 
     this.mSurfaceHolder = getHolder(); 
     this.mSurfaceHolder.addCallback(this); 

     // THIS IS WHERE THE FINDVIEWBYID IS RETURNING NULL 
     this.mFPSTextView = (TextView) findViewById(R.id.fps_text_view); 

     setFocusable(true); 

     this.mThread = new RoadThread(this); 
    } 

У вас есть люди на свете? Я уже пытался вызвать findViewById в другом методе после завершения конструктора, но без успеха. Спасибо,

+0

'' findViewById в вашей деятельности становится затмил один в этом внутреннем классе. Он выполняет поиск по дочерним представлениям, а ваше представление поверхности не имеет дочерних представлений. Используйте 'SplashScreen.this.findViewById()' для перехода в область действия только для этого метода. –

+0

Спасибо, ответ на ваш вопрос Грег. Это не внутренний класс, а отдельный. Как я показал выше, это одно и то же, но его нет. Я попробовал то, что ребята сказали ниже: mFPSTextView = (TextView) ((SplashScreen) mContext) .findViewById (R.id.fps_text_view); муравей это сработало. – Daniel

ответ

0

Посмотреть findViewById-х ищет для ребенка данного представления не в макете это деятельность по. Таким образом, вы должны вызвать активность в findViewById

((Activity) getContext()).findViewById(R.id.fps_text_view) 
Смежные вопросы