如何在我的Android应用的Java代码中将NumberPicker添加到我的布局中?

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

How can I add NumberPicker on my Layout from Java code in my Android application?

问题

我正在制作一个简单的应用程序我想从我的Java代码中添加NumberPicker但是当我运行它时应用程序会立即关闭
有人能告诉我错误出在哪里吗
我知道我可以在XML中添加它但我想从Java代码中添加它

非常感谢

package com.example.openwebpage;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.NumberPicker;

public class MainActivity extends AppCompatActivity {
    NumberPicker np;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.layout);
        String[] displayed = {"1","2","3"};

        np = new NumberPicker(this);
        ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        np.setLayoutParams(layoutParams);

        np.setDisplayedValues(displayed);
        np.scrollBy(0,3);
        np.setMinValue(0);
        np.setMaxValue(3);

        np.setWrapSelectorWheel(true);

        layout.addView(np);


    }
}
英文:

I am making a simple application. I want to add NumberPicker from my Java code, but when I run it, the applications closes immediately.
Can someone tell me where is the error?
I know that I can add it in XML, but I wanted to add it from Java code.

Thanks a lot

package com.example.openwebpage;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.NumberPicker;

public class MainActivity extends AppCompatActivity {
    NumberPicker np;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.layout);
        String[] displayed = {"1","2","3"};

        np = new NumberPicker(this);
        ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        np.setLayoutParams(layoutParams);

        np.setDisplayedValues(displayed);
        np.scrollBy(0,3);
        np.setMinValue(0);
        np.setMaxValue(3);

        np.setWrapSelectorWheel(true);

        layout.addView(np);


    }
}

答案1

得分: 0

你需要更新这段代码片段:

np.scrollBy(0,2);
np.setMinValue(0);
np.setMaxValue(2);

异常是由于 setMaxValue(3) 导致的数组索引越界。

英文:

You need to update this code snippet:

np.scrollBy(0,2);
np.setMinValue(0);
np.setMaxValue(2);

The exception was Array Indexed Out of bound due to setMaxValue(3).

huangapple
  • 本文由 发表于 2020年10月20日 02:32:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/64433243.html
匿名

发表评论

匿名网友

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

确定