Android 预设号码选择器
这是我在 stackoverflow 上的第一篇文章,所以如果我做错了什么,请随时告诉我。
我正在制作一个 Android 应用程序。对于我的一个菜单活动,我需要某种小部件,允许用户从预定义集中选择一个数字。 (即:{50, 100, 200, 500, 1000, 2000})
我查看了 Spinner、SeekBar 和 NumberPicker 小部件,以下是我发现的每个小部件的问题。
- Spinner 似乎只允许使用字符串。
- SeekBar 和 NumberPicker 似乎不容易允许预设值。
这些小部件中的任何一个都可以满足我想要做的事情吗?如果没有,是否有其他小部件可能工作得更好?如果没有,那么你会建议我如何去做呢?
谢谢!
编辑:
我尝试使用旋转器和Integer.parseInt
函数。我遇到了空指针异常:
02-11 18:21:23.823: E/AndroidRuntime(359): Caused by: java.lang.NullPointerException
02-11 18:21:23.823: E/AndroidRuntime(359): at com.Package.Jigsaw.PuzzleActivity.onCreate(PuzzleActivity.java:20)
PuzzleActivity 的第 20 行是我声明 numPc 的行。
以下是相关代码:
菜单活动:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_puzzle);
//final Gallery gal = (Gallery) findViewById(R.id.opt_img);
final Spinner numPc = (Spinner) findViewById(R.id.opt_numpc);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.opt_numpc_options, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
numPc.setAdapter(adapter);
...snip...
Button start_btn = (Button) findViewById(R.id.start_puzzle_btn);
start_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent("us.kniffin.Jigsaw.OPENPUZZLE");
//i.putExtra("img", gal.getSelectedItem());
//i.putExtra("numPc", numPc.getSelectedItem());
i.putExtra("numPc", Integer.parseInt(numPc.getSelectedItem().toString()));
//i.putExtra("rot", rot.getSelectedItem().toString());
//i.putExtra("connType", connType.getSelectedItem().toString());
startActivity(i);
}
});
主要活动(PuzzleActivity):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final PuzzleView v;
Dimension numPcXY;
// TODO: update this line to read in options
int numPc = (Integer) savedInstanceState.get("numPc");
// TODO: update this line to read in options
picture = BitmapFactory.decodeResource(getResources(), R.drawable.test_pic);
picture = Bitmap.createScaledBitmap(picture, picture.getWidth()/3, picture.getHeight()/3, false);
// Do some math to do a case statement to determine numPcX and numPcY
numPcXY = calcXYpieces(numPc, picture);
// Create the puzzle
pieces = new PuzzlePieceSet(picture, numPcXY.getX(), numPcXY.getY());
v = new PuzzleView(this, pieces);
setContentView(v);
pieces.scramble();
}
This is my first post on stackoverflow, so feel free let me know if I'm doing anything wrong.
I'm working on making an android app. For one of my menu activity's I need some sort of widget that will allow the user to pick a number from a predefined set. (ie: {50, 100, 200, 500, 1000, 2000})
I looked at the Spinner, SeekBar, and NumberPicker Widgets, and here's the problems I've found with each.
- The Spinner seems to only allow strings.
- The SeekBar and NumberPicker don't seem to easily allow preset values.
Would any of these widgets work for what I'm trying to do? If not, is there another widget that might work better? If not, then how would you recommend I go about this?
Thanks!
Edit:
I tried using a spinner and the Integer.parseInt
function. I got a null pointer exception:
02-11 18:21:23.823: E/AndroidRuntime(359): Caused by: java.lang.NullPointerException
02-11 18:21:23.823: E/AndroidRuntime(359): at com.Package.Jigsaw.PuzzleActivity.onCreate(PuzzleActivity.java:20)
Line 20 of PuzzleActivity is the one where I'm declaring numPc.
Here's the relevant bits of code:
Menu Activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_puzzle);
//final Gallery gal = (Gallery) findViewById(R.id.opt_img);
final Spinner numPc = (Spinner) findViewById(R.id.opt_numpc);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.opt_numpc_options, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
numPc.setAdapter(adapter);
...snip...
Button start_btn = (Button) findViewById(R.id.start_puzzle_btn);
start_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent("us.kniffin.Jigsaw.OPENPUZZLE");
//i.putExtra("img", gal.getSelectedItem());
//i.putExtra("numPc", numPc.getSelectedItem());
i.putExtra("numPc", Integer.parseInt(numPc.getSelectedItem().toString()));
//i.putExtra("rot", rot.getSelectedItem().toString());
//i.putExtra("connType", connType.getSelectedItem().toString());
startActivity(i);
}
});
Main Activity (PuzzleActivity):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final PuzzleView v;
Dimension numPcXY;
// TODO: update this line to read in options
int numPc = (Integer) savedInstanceState.get("numPc");
// TODO: update this line to read in options
picture = BitmapFactory.decodeResource(getResources(), R.drawable.test_pic);
picture = Bitmap.createScaledBitmap(picture, picture.getWidth()/3, picture.getHeight()/3, false);
// Do some math to do a case statement to determine numPcX and numPcY
numPcXY = calcXYpieces(numPc, picture);
// Create the puzzle
pieces = new PuzzlePieceSet(picture, numPcXY.getX(), numPcXY.getY());
v = new PuzzleView(this, pieces);
setContentView(v);
pieces.scramble();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我个人会使用 Spinner,然后对结果调用 Integer.parseInt 来获取您需要的 int。
好的,现在有了更多代码,请查看这个答案(它使用字符串,但对于整数是相同的),您错误地使用了 putExtra 东西。
请参阅如何对字符串数据使用 putExtra() 和 getExtra() 了解更多信息。
I personally would use a Spinner and then call Integer.parseInt on the result to get the int you need.
Ok, now with more of your code, please look at this answer (which uses a String, but it is the same for an Integer), you are using the putExtra stuff incorrectly.
See How to use putExtra() and getExtra() for string data for more information.
我弄清楚了我的问题。
问题出在这一行:
我应该使用这样的东西:
I figured out my issue.
The problem lies in this line:
I should be using something like this: