fragment 里需要activity。Android-The method findViewById(int) is undefined for the type first (Fragment)

部分参考:  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……


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值