【问题标题】:Android-The method findViewById(int) is undefined for the type first (Fragment)Android - 方法findViewById(int) 未定义类型优先(Fragment)
【发布时间】:2014-01-25 17:13:12
【问题描述】:

我是 Fragment 的新手,我正在使用带有标签的 Swipe 视图开发应用程序。我的目标是让 textview 显示存储在字符串数组中的文本,并在应用程序重新启动时更改。 但是当我使用 findViewById 时似乎有问题。

代码:

First.java

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]);


    }

}

2.first_xml.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#fa6a6a" >

<TextView android:layout_width="fill_parent"
    android:id="@+id/text1"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@array/quote"
    android:textSize="40dp"
    android:layout_centerInParent="true"/>


</RelativeLayout>

任何帮助将不胜感激。 如果我提到的任何内容不够清楚,请告诉我。 谢谢!

【问题讨论】:

    标签: android xml random android-fragments textview


    【解决方案1】:

    Fragment 类没有findViewById(...) 方法,因此您必须从rootViewActivity 获取您的意见。我建议让您的 TextView 成为您的 Fragment 的成员,从您的 rootView 中检索它,并根据需要引用它。

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

    (已编辑以删除对 findViewById 的冗余调用。)

    【讨论】:

      【解决方案2】:

      在 onCreateView() 中保留 inflater.inflate() 返回的根视图。

      这可以稍后用于调用 findViewById() 以根据要求查找任何视图。

      View mView;
      
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          mView = inflater.inflate(R.layout.xyz, container, false);
      
          return mView;
      }
      

      然后在班级的任何地方

      mView.findViewById(R.id.some_view);
      



      或者,如果你可以使用

      getActivity().findViewById(R.id.xyz);
      

      【讨论】:

      • getActivity() 就是这么简单。谢谢~
      【解决方案3】:

      我建议您在片段中创建一个TextView 属性并在您的onCreateView 方法中分配它并使用这句话myTextView = (TextView) rootView.findViewById(R.id.text1);,然后在您的refreshTV 方法中使用它。

      希望对你有帮助

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多