每次我在NumberPicker上的EditText或getValue()上使用getText()时,都会抛出此异常(或接近变化)。我已经设法使用getText()方法使一个应用程序工作,但我不能在我的生活中辨别出成功的代码和所有其他失败之间的区别。有什么建议?我已经为我的非工作号码选择器提供了代码。
package com.example.daniel.forjoe;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.NumberPicker;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NumberPicker redVal = (NumberPicker) findViewById(R.id.RedPicker);
NumberPicker greenVal = (NumberPicker) findViewById(R.id.GreenPicker);
NumberPicker blueVal = (NumberPicker) findViewById(R.id.BluePicker);
SetBounds(redVal);
SetBounds(greenVal);
SetBounds(blueVal);
}
public void ChangeColor (View v) {
TextView smiles = (TextView) v.findViewById(R.id.smilez);
NumberPicker redVal = (NumberPicker) v.findViewById(R.id.RedPicker);
NumberPicker greenVal = (NumberPicker) v.findViewById(R.id.GreenPicker);
NumberPicker blueVal = (NumberPicker) v.findViewById(R.id.BluePicker);
int red = redVal.getValue();
int green = greenVal.getValue();
int blue = blueVal.getValue();
int bgIntColor = argb(255, red, green, blue);
smiles.setBackgroundColor(bgIntColor);
setContentView(R.layout.color_page);
}
public void SwitchBack (View v) {
setContentView(R.layout.activity_main);
}
public int argb (int alpha, int red, int green, int blue) {
return Color.argb(alpha, red, green, blue);
}
public NumberPicker SetBounds (NumberPicker np) {
np.setMaxValue(255);
np.setMinValue(0);
return np;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:1)
最有可能的问题出在你的ChangeColor
方法中(如果没有布局xml,我无法确定)。在这里,您实际上是在尝试查找传递给方法而不是根内容视图的View v
的子视图。
public void ChangeColor (View v) {
TextView smiles = (TextView) v.findViewById(R.id.smilez);
NumberPicker redVal = (NumberPicker) v.findViewById(R.id.RedPicker);
NumberPicker greenVal = (NumberPicker) v.findViewById(R.id.GreenPicker);
NumberPicker blueVal = (NumberPicker) v.findViewById(R.id.BluePicker);
...
您应该用
替换上面的代码public void ChangeColor (View v) {
TextView smiles = (TextView) findViewById(R.id.smilez);
NumberPicker redVal = (NumberPicker) findViewById(R.id.RedPicker);
NumberPicker greenVal = (NumberPicker) findViewById(R.id.GreenPicker);
NumberPicker blueVal = (NumberPicker) findViewById(R.id.BluePicker);
...