简体   繁体   中英

Seekbar is null, even though it exists

My goal is to access a seekbar. I tried many things, even pulling the Seekbar seekingbar before the override, but I get always the null toast. R.layout.firstLayout , R.layout.secondLayout , R.id.seekingbar , they all exist.

This is my abbreviated code

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>

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM