包含android的布局控件显示空指针异常

包含android的布局控件显示空指针异常,android,Android,我已经尝试了许多布局充气机等东西。 但无法访问切换按钮控件。 PostActivity.java protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_post); View shareView = findViewById(R.id.in_shareLayout);

我已经尝试了许多布局充气机等东西。 但无法访问切换按钮控件。 PostActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_post);

        View shareView = findViewById(R.id.in_shareLayout);


 tbFb = (ToggleButton) shareView.findViewById(R.id.tbFb);
   tbFb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // Save the state here
                Log.e("I Am Tired!", "tired--> " + isChecked);
            }
        });
        tbFb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.e("I Am Tired!", "tired--> ");

            }
        });
public class DemoActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout2);

        ToggleButton tb = (ToggleButton) findViewById(R.id.tbFb);
        tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.e("Demo", "===================="+isChecked);
            }
        });
    }
}
在上面的类中,不会激发onClick或checked更改的侦听器。 它总是显示错误的值

包含布局sharecontent\u Layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="20dp"
    android:textSize="14dp"
    android:text="share on"
    />
<ToggleButton
    android:layout_width="39dp"
    android:layout_height="39dp"
    style="@style/fbtoggleButton"
    android:background="@drawable/fbsharebg"
    android:id="@+id/tbFb"/></LinearLayout>

这是我的布局PostActivity.xml

 <LinearLayout
            android:id="@+id/share_layout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_gravity="center_horizontal"
            android:layout_weight="10"
            android:orientation="vertical"
            android:paddingLeft="10dp"
            android:paddingRight="10dp">

            <include
                android:id="@+id/in_shareLayout"
                layout="@layout/sharecontent_layout" />
        </LinearLayout>

您需要传递布局文件名

更改您的:

layout="@layout/sharecontent_layout"


使用以下命令:

layout1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="20dp"
        android:text="share on"
        android:textSize="14dp" />

    <ToggleButton
        android:id="@+id/tbFb"
        android:layout_width="39dp"
        android:layout_height="39dp" />
</LinearLayout>

上面的代码是为我编写的,我已经测试过了

您无法通过这种方式找到视图

View shareView = findViewById(R.id.in_shareLayout);
去掉这个

您可以直接访问包含的布局控件

tbFb = (ToggleButton) shareView.findViewById(R.id.tbFb);

我还尝试显示java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.ToggleButton.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)'在空对象引用上,您必须从tbFb中删除setOnClickListener。未解决相同错误java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.ToggleButton.setOnCheckedChangeListener(android.widget.CompoundButton$OnCheckedChangeListener)'在空对象引用上,我还在包含的布局中使用了merge标记,也没有得到tbFb.isChecked()值。它显示空引用错误。请发布日志。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">


    <include
        android:id="@+id/in_shareLayout"
        layout="@layout/layout1" />
</LinearLayout>
public class DemoActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout2);

        ToggleButton tb = (ToggleButton) findViewById(R.id.tbFb);
        tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Log.e("Demo", "===================="+isChecked);
            }
        });
    }
}
View shareView = findViewById(R.id.in_shareLayout);
tbFb = (ToggleButton) shareView.findViewById(R.id.tbFb);