Seekbar为null,尽管它存在。

huangapple go评论77阅读模式
英文:

Seekbar is null, even though it exists

问题

我的目标是访问一个拖动条我尝试了很多方法甚至在覆盖之前拉取了Seekbar seekingbar但我始终收到空的toast通知。`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("前进", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                AlertDialog dialog2 = new AlertDialog.Builder(MainActivity.this)
                                        .setView(secondLayout)
                                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                dialog.dismiss();

                                                if (seekingbar == null)
                                                {
                                                    Toast.makeText(MainActivity.this, "空值", 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>
英文:

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(&quot;Forward&quot;, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
AlertDialog dialog2 = new AlertDialog.Builder(MainActivity.this)
.setView(secondLayout)
.setPositiveButton(&quot;Okay&quot;, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
if (seekingbar == null)
{
Toast.makeText(MainActivity.this,&quot;Null&quot;,Toast.LENGTH_LONG).show();
}
}
}).create();
dialog2.show();
}
}).create();
dialog.show();
}
});
}
}

firstLayout.xml

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

答案1

得分: 0

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

英文:

Try to find the seekBar in the firstLayout after declaring it:
"firstLayout.findViewById(R.id.seekbar)"

huangapple
  • 本文由 发表于 2020年10月4日 23:06:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64196250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定