在我的oncreate方法中,我有:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_activity);
mContext = this;
}
mContext
Context
对于整个类都是静态的。
但是,即使我这样做,我也无法做到这一点:
mContext.findViewById(R.id.score_text);
它说它无法解析方法“ findViewbyId”
我究竟做错了什么?
mContext是整个类的静态Context
请删除此。
我究竟做错了什么?
findViewById()
是对一个方法Activity
和Fragment
。这不是的方法Context
。
由于您正在调用setContentView()
,因此您的onCreate()
方法处于活动状态,因此您只需调用findViewById()
:
/***
Copyright (c) 2012 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not
use this file except in compliance with the License. You may obtain a copy
of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
by applicable law or agreed to in writing, software distributed under the
License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
OF ANY KIND, either express or implied. See the License for the specific
language governing permissions and limitations under the License.
From _The Busy Coder's Guide to Android Development_
https://commonsware.com/Android
*/
package com.commonsware.android.extra;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class OtherActivity extends Activity {
public static final String EXTRA_MESSAGE="msg";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
TextView tv=(TextView)findViewById(R.id.msg);
tv.setText(getIntent().getStringExtra(EXTRA_MESSAGE));
}
}
(来自此示例项目)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句