【问题标题】:Seekbar is null, even though it existsSeekbar 为空,即使它存在
【发布时间】:2020-10-04 15:06:04
【问题描述】:

我的目标是访问搜索栏。我尝试了很多东西,甚至在覆盖之前拉了 Seekbar 搜索栏,但我总是得到空吐司。 R.layout.firstLayoutR.layout.secondLayoutR.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();
            }
        });
    }
}

firstLayout.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>

【问题讨论】:

  • 我更新了我的帖子..

标签: java android nullpointerexception seekbar android-seekbar


【解决方案1】:

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

【讨论】:

  • 这就是解决方案。
猜你喜欢
  • 1970-01-01
  • 2017-09-28
  • 2016-08-06
  • 1970-01-01
  • 2022-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-15
相关资源
最近更新 更多