findviewbyid - 无法解决

时间:2016-03-21 18:28:42

标签: java android xml android-fragments

这是我在navigation drawer中使用的一个片段。我需要添加一个按钮,按下后,将文本框更改为不同的文本。我不断收到findviewbyid() int无法解决的错误等等。我可以在MainActivity/activity_main上使用它,但是当我使用导航抽屉的一个页面(如ConnectFragment.java/fragment_connect.xml)时,我会收到错误。

这是仅ConnectFragment的代码

import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class ConnectFragment extends Fragment {
    public ConnectFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_connect, container, false);

        return rootView;
    }       
}

这是我想要实现的代码

final Button ClassAButton = (Button)findViewById(R.id.ClassAButton);

ClassAButton.setOnClickListener(
    new Button.OnClickListener(){
        public void onClick(View view){
            TextView FeloniesText = (TextView)findViewById(R.id.FeloniesText);
            FeloniesText.setText("Button 1 Has been pressed!");
        }
    }
);

来自XML的代码(这里的相对布局标签,格式化搞乱了)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/backgroundColor">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/ClassAButton"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Large Text"
        android:id="@+id/FeloniesText"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="107dp" />
</RelativeLayout>

我还想添加多个按钮来将文本视图更改为不同的东西,所以我会像,A类按钮打印出来,B类按钮打印出来,C类按钮打印出大象等等。帮助将不胜感激。 :D BTW API 15

2 个答案:

答案 0 :(得分:3)

试试这个。

更改此行。

final Button ClassAButton = (Button)findViewById(R.id.ClassAButton);

对此。

final Button ClassAButton = (Button) rootView.findViewById(R.id.ClassAButton);

TextView FeloniesText = (TextView) rootView.findViewById(R.id.FeloniesText);

编辑:对于文字视图,请尝试此操作。

ClassAButton.setOnClickListener(new View.OnClickListener(){
                public void onClick(View view){
                    FeloniesText.setText("Button 1 Has been pressed!");
                }
            }
    );

在代码中替换它。

编辑2:

将此完整代码替换为您的代码。

在参考链接中,他们已经采取了活动,你已经采取片段。

public class ConnectFragment extends Fragment {

final Button ClassAButton;
TextView FeloniesText;



public ConnectFragment() {
}

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

    View rootView = inflater.inflate(R.layout.fragment_connect, container, false);
    init(rootView);
    return rootView;

}


public void init(View view)
{
    ClassAButton = (Button) view.findViewById(R.id.ClassAButton);
    FeloniesText = (TextView) view.findViewById(R.id.FeloniesText);
    ClassAButton.setOnClickListener(new View.OnClickListener(){
                public void onClick(View view){
                    FeloniesText.setText("Button 1 Has been pressed!");
                }
            }
    );
}

答案 1 :(得分:1)

findViewById不属于Fragment。它是ViewGroup的一部分,而不是

(TextView)findViewById(R.id.FeloniesText);

你需要

(TextView)rootView.findViewById(R.id.FeloniesText);
相关问题