Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/223.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java findViewById在片段内的静态函数中不工作_Java_Android_Android Fragments - Fatal编程技术网

Java findViewById在片段内的静态函数中不工作

Java findViewById在片段内的静态函数中不工作,java,android,android-fragments,Java,Android,Android Fragments,在我的android应用程序中,我有一个片段1。在该片段中,我有一个静态函数“function1”。我试图在该静态函数中使用 button=(Buton)getActivity().findViewById(R.id.button1); 但是Eclipse抛出了一个错误,比如“无法从类型片段对非静态方法getActivity()进行静态引用”。我做错了什么?我需要这个函数1是静态的,这样我可以从另一个类调用它。我的意思是,当我选择一个特定的片段时,我必须从主活动填充列表视图 片段1=> pub

在我的android应用程序中,我有一个片段1。在该片段中,我有一个静态函数“function1”。我试图在该静态函数中使用

button=(Buton)getActivity().findViewById(R.id.button1);
但是Eclipse抛出了一个错误,比如“无法从类型片段对非静态方法getActivity()进行静态引用”。我做错了什么?我需要这个函数1是静态的,这样我可以从另一个类调用它。我的意思是,当我选择一个特定的片段时,我必须从主活动填充列表视图

片段1=>

public class Fragment1 extends Fragment{


public static String feedurl="http://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml";
static String URL = "";
static final String KEY_HEAD = "item"; // parent node
static final String KEY_DATE = "pubDate";
public static String headflag="";
int f=0;
static Button button;
    HeadlinesAdapter adapter;
    private TextView mMessageView;
private Button mClearButton;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.first_fragment, container, false);


    return v;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);



    function1();
    populate_listview();

    }


 public static void function1()
 {


     URL="http://www.abcd.com/en/rssfeeds/1_2_3_5/latest/rss.xml";
     ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();

    XMLParser parser = new XMLParser();
    String xml = parser.getXmlFromUrl(URL);
    Document doc = parser.getDomElement(xml);
    NodeList nl = doc.getElementsByTagName(KEY_HEAD);
    NodeList itemLst = doc.getElementsByTagName("item");
    String MarqueeStr="";

    for (int i = 0; i < nl.getLength(); i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        Element e = (Element) nl.item(i);
                    newsList.add(map);


 }
    button=(Buton)getActivity().findViewById(R.id.button1);;// Here it shows error.
    adapter=new Adapter1(getActivity(), newsList);        
            list.setAdapter(adapter);

  }

function1()
的签名更改为:

public static void function1(Fragment f)
和使用:

button = (Buton) f.getActivity().findViewById(R.id.button1);
而不是:

button = (Buton) getActivity().findViewById(R.id.button1);
并按如下方式调用该方法:

function1(this);


但是,如果此方法没有任何用途,则它应该是实例方法而不是静态方法。

不要使用静态方法,因为它不能引用片段1实例,除非您将其作为输入参数提供。此外,更改按钮的查找结果如下:

button = (Buton) getView().findViewById(R.id.button1);
您目前有:

static Button button;
您应该删除static关键字,改为写入:

private Button button;

这里没有理由让任何东西都是静态的(除了大写的常量,这些常量应该同时声明为
static
final

从代码中删除所有
静态
关键字,它应该可以工作(除了大写的变量):

@凌驾 创建视图上的公共视图(布局、充气机、视图组容器、, Bundle savedInstanceState){


}

您必须将该特定布局充气,然后使用该布局查找id

**


**

function1()
似乎不是静态的!这个答案是正确的,但您不应该鼓励这样滥用static关键字。这里没有理由让这些方法是静态的。@MarvinLabs您是对的,但OP似乎需要一个静态方法(见标题),因此我给了他相应的解决方案。我无法从主活动调用函数……它显示“Fragment1类型中的方法函数1(Fragment)不适用于参数(新建ViewPager.OnPageChangeListener(){})我需要从另一个类调用function1。所以它必须是静态的否?无论如何,您都需要将此静态函数传递给片段的实例。因此,您也可以在实例本身上调用该函数。在您的问题中添加一些关于如何从其他地方调用该函数的详细信息,以便我们可以建议重构。要获取当前片段,请参阅
private Button button;
private Button button;

// ...

public void function1() {
  // ...
}
View v = inflater.inflate(R.layout.first_fragment, container, false);
button=(Button) v.findViewById(R.id.button1);


return v;
View v = inflater.inflate(R.layout.first_fragment, container, false);
button=(Button) v.findViewById(R.id.button1);
return v;