部分参考: http://stackoverflow.com/questions/20981487/android-the-method-findviewbyidint-is-undefined-for-the-type-first-fragment/20981583
I'm new to fragments and i'm developing an application using Swipe view with tabs. My aim here is to get a textview display texts stored in the string-array and changes whenever the application is restarted. But there seems to be a problem when i'm using findViewById.
import java.util.Random;
import android.support.v4.app.Fragment;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class first extends Fragment{
String[] strArr;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.first_xml, container, false);
strArr = getResources().getStringArray(R.array.quote);
//quote is the name given to the string array
return rootView;
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
refreshTV();
}
void refreshTV(){
TextView tv = (TextView)findViewById(R.id.text1);
Random ran = new Random();
int c = ran.nextInt(strArr.length);
tv.setText(strArr[c]);
}
}
Any help will be appreciated. Please Let me know if anything i mentioned is not clear enough. Thank you !
方案:
The Fragment
class does not have the findViewById(...)
method, so you have to get your views from your rootView
or your Activity
. I would suggest making your TextView
a member of your Fragment
, retrieving it from your rootView
, and referencing it as needed.
public class first extends Fragment {
String[] strArr;
TextView tv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView = inflater.inflate(R.layout.first_xml, container, false);
tv = rootView.findViewById(R.id.text1);
strArr = getResources().getStringArray(R.array.quote);
//quote is the name given to the string array
return rootView;
}
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
refreshTV();
}
void refreshTV(){
Random ran = new Random();
int c = ran.nextInt(strArr.length);
tv.setText(strArr[c]);
}
}
fragment里面 如果有需要context的地方,应该是由 该fragment 里面 建一个单参数的构造函数,将activity变量穿进来。如:
public Findfragment(Activity activity){
this.activity = activity;
}
下面需要用的context的地方: 以activity 表示
TextSliderView textSliderView = new TextSliderView(activity);
Toast.makeText(activity, slider.getBundle().get("extra") + "",
Toast.LENGTH_SHORT).show();
Context字面意思上下文,位于framework package的android.content.Context中,其实该类为LONG型,类似Win32中的Handle句柄,很多方法需要通过 Context才能识别调用者的实例,比如说Toast的第一个参数就是Context,一般在Activity中我们直接用this代替,代表调用者的实例为Activity,而到了一个button的onClick(View view)等方法时,我们用this时就会报错,所以我们可能使用ActivityName.this来解决,主要原因是因为实现Context的类主要有Android特有的几个模型,Activity、Service以及BroadcastReceiver。
常规需要Context实例的方法主要有各种Service实现的类,比如说SensorManager在实例化时需要 getSystemService(String)方法就必须由Context的实例执行,还有一些私有的文件系统I/O比如说 openFileInput以及常用的Toast的makeText方法。
其实很多人也和你一样,都对context做过一些探讨的。
不过我看过的最简单的理解:
一个APK进程只有一个Context: 这个Context就是ApplicationContext,从Context继承过来。
ApplicationContext可以看做是针对整个系统的全局处理接口,因为:
它负责和系统服务打交道
RPC通信由他通过那些XXXServiceManager和XXXService来处理。
其他一些模块,比如Activity……