首页 > 解决方案 > Seekbar 为空,即使它存在

问题描述

我的目标是访问搜索栏。我尝试了很多东西,甚至在覆盖之前拉了 Seekbar 搜索栏,但我总是得到空吐司。R.layout.firstLayout, R.layout.secondLayout, R.id.seekingbar, 它们都存在。

这是我的缩写代码

public class MainActivity extends AppCompatActivity {

    SeekBar seekingbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        seekingbar = (SeekBar) findViewById(R.id.seekingbar);
        LayoutInflater inflater = getLayoutInflater();
        final View firstLayout = inflater.inflate(R.layout.firstLayout, null);
        final View secondLayout = inflater.inflate(R.layout.secondLayout, null);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
                        .setPositiveButton("Forward", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                AlertDialog dialog2 = new AlertDialog.Builder(MainActivity.this)
                                        .setView(secondLayout)
                                        .setPositiveButton("Okay", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                dialog.dismiss();

                                                if (seekingbar == null)
                                                {
                                                    Toast.makeText(MainActivity.this,"Null",Toast.LENGTH_LONG).show();
                                                }
                                            }
                                        }).create();
                                dialog2.show();
                            }
                        }).create();
                dialog.show();
            }
        });
    }
}

第一个布局.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/seekbarll"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    tools:context=".MainActivity">
    
    <SeekBar
        android:id="@+id/seekingbar"
        style="@style/Widget.AppCompat.SeekBar.Discrete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="10"
        android:min="0"
        android:progress="3" />

</LinearLayout>

标签: javaandroidnullpointerexceptionseekbarandroid-seekbar

解决方案


声明后尝试在firstLayout中找到seekBar:“firstLayout.findViewById(R.id.seekbar)”


推荐阅读