簡體   English   中英

Android-方法findViewById(int)對於類型first(片段)未定義

[英]Android-The method findViewById(int) is undefined for the type first (Fragment)

我是片段的新手,並且正在使用帶有選項卡的滑動視圖來開發應用程序。 我的目的是使文本視圖顯示存儲在字符串數組中的文本,並在應用程序重新啟動時進行更改。 但是當我使用findViewById時似乎出現了問題。

碼:

第一.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>

任何幫助將不勝感激。 如果我提到的內容不夠清楚,請讓我知道。 謝謝 !

Fragment類沒有findViewById(...)方法,因此您必須從rootViewActivity獲取視圖。 我建議將TextView rootView 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的多余調用。)

將inflater.inflate()返回的根視圖保留在onCreateView()中。

以后可以使用它調用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);

我建議您在片段中創建一個TextView屬性,並在onCreateView方法中對其進行分配,並使用以下語句: myTextView = (TextView) rootView.findViewById(R.id.text1); 然后在您的refreshTV方法中使用它。

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM