【问题标题】:findviewbyid - Cannot Resolvefindviewbyid - 无法解决
【发布时间】:2016-07-08 10:14:19
【问题描述】:

这是我在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 类按钮打印出 hi,B 类按钮打印出 yes,C 类按钮打印出大象,等等。任何帮助将不胜感激。 :D BTW API 15

【问题讨论】:

    标签: java android xml android-fragments


    【解决方案1】:

    findViewById 不是Fragment 的一部分。它是ViewGroup 的一部分,所以不是

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

    你需要

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

    【讨论】:

    • TextView FeloniesText = (TextView)rootView.findViewById(R.id.FeloniesText);当前出现错误,如:变量“根视图”是从内部类中访问的,需要声明为最终我只在这一行出现此错误,而不是其他行
    【解决方案2】:

    试试这个。

    改变这一行。

    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!");
                    }
                }
        );
    }
    

    【讨论】:

    • 感谢您的帮助,代码可以正常工作并且没有更多错误,但是在按下按钮时,应用程序崩溃了,求助?
    • 致命异常:主进程:nota.outlawsindex,PID:3236 java.lang.NullPointerException:尝试调用虚拟方法 'void android.widget.TextView.setText(java.lang.CharSequence)' on nota.outlawsindex.ConnectFragment$1.onClick(ConnectFragment.java:33) 处的空对象引用
    • @TyreseBrown 检查我已编辑的答案,我已在点击监听器上更新。
    • 是的,一旦使用您更新的代码,应用仍然会在按下按钮时崩溃
    • @TyreseBrown 检查我编辑的答案。我已经从点击监听器中删除了文本视图。并将其定义为下面的类按钮。
    猜你喜欢
    • 1970-01-01
    • 2016-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    相关资源
    最近更新 更多