lol*_*ubs -1 android nullpointerexception
错误是:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{lol2dubs.stevemoa/lol2dubs.stevemoa.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference
Run Code Online (Sandbox Code Playgroud)
代码是:
public class MainActivity extends AppCompatActivity {
double MOA;
TextView turretClicks = (TextView)findViewById(R.id.turretClicks);
boolean noMOA;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText range = (EditText) findViewById(R.id.rangeEntry);
String stringRange = range.getText().toString();
int finalRange = Integer.parseInt(stringRange);
if(finalRange <= 200) {
MOA = 0;
}
if(finalRange > 200 && finalRange <= 225){
MOA = .5;
}
if(finalRange > 225 && finalRange <= 250) {
MOA = 1;
}
if(finalRange > 250 && finalRange <= 275) {
MOA = 1.65;
}
if(finalRange > 275 && finalRange <= 300) {
MOA = 2.25;
}
if(finalRange > 300 && finalRange <= 325) {
MOA = 2.8;
}
if(finalRange > 325 && finalRange <= 350) {
MOA = 3.5;
}
if(finalRange > 350 && finalRange <= 375) {
MOA = 4.0;
}
if(finalRange > 375 && finalRange <= 400) {
MOA = 4.75;
}
if(finalRange > 400 && finalRange <= 425) {
MOA = 5.50;
}
if(finalRange > 425 && finalRange <= 450) {
MOA = 6.25;
}
if(finalRange > 450 && finalRange <= 475) {
MOA = 7.0;
}
if(finalRange > 475 && finalRange <= 500) {
MOA = 7.5;
}
if(finalRange > 500 && finalRange <= 525) {
MOA = 8.25;
}
if(finalRange > 525 && finalRange <= 550) {
MOA = 9.0;
}
if(finalRange > 550 && finalRange <= 575) {
MOA = 9.75;
}
if(finalRange > 575 && finalRange <= 600) {
MOA = 10.5;
}
if(finalRange > 600 && finalRange <= 625) {
MOA = 11.5;
}
if(finalRange > 625 && finalRange <= 650) {
MOA = 12.25;
}
if(finalRange > 650 && finalRange <= 675) {
MOA = 13;
}
if(finalRange > 675 && finalRange <= 700) {
MOA = 14;
}
if(finalRange > 700) {
noMOA = true;
}
// Create an anonymous implementation of OnClickListener
View.OnClickListener btnClickCalc = new View.OnClickListener() {
public void onClick(View v) {
double clicks = (MOA * 4);
String toText = Double.toString(clicks);
turretClicks.setText(toText);
}
};
// Capture our button from layout
Button button = (Button)findViewById(R.id.btnClickCalc);
// Register the onClick listener with the implementation above
button.setOnClickListener(btnClickCalc);
}
}
Run Code Online (Sandbox Code Playgroud)
基本上,我正在为我的岳父制作一个简单的应用程序,所以他不必在他的步枪炮塔上计算MOA.他的范围是四分之一的MOA,所以如果他的射门距离是550码,他必须使用9次MOA,这是9次4次点击.
我有一个名为rangeEntry的输入范围EditText我有一个名为btnClickCalc的按钮我有一个名为turretClicks的TextView,它应该显示所述范围所需的点击次数.
我错过了什么错误?我看到这是因为某些东西是空的......我看到其他文章说他们没有实例化某些东西......但我没看到我的问题是什么.
任何人?
XML是:
<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate MOA"
android:id="@+id/textView"
android:textColor="#ff0000"
android:textSize="28dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculate Clicks"
android:id="@+id/btnClickCalc"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#ff0000"
android:onClick="calcClick" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/turretClicks"
android:textSize="28dp"
android:textColor="#ff0000"
android:hint="0"
android:background="#ffffff"
android:layout_below="@+id/btnClickCalc"
android:layout_centerHorizontal="true"
android:layout_marginTop="74dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/rangeEntry"
android:layout_marginTop="91dp"
android:textSize="20dp"
android:hint="Enter Range"
android:background="#ffffff"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clicks"
android:id="@+id/textView2"
android:layout_below="@+id/turretClicks"
android:layout_centerHorizontal="true"
android:background="#ffffff"
android:textColor="#fc0000"
android:textSize="20dp" />
Run Code Online (Sandbox Code Playgroud)
下面的解决方案修复了原始错误,但我现在得到:
10-31 22:11:04.754 10019-10019/lol2dubs.stevemoa E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{lol2dubs.stevemoa/lol2dubs.stevemoa.MainActivity}: java.lang.NumberFormatException: Invalid int: ""
Run Code Online (Sandbox Code Playgroud)
移动你的Textview定义中onCreate()之后setContentView().
像这样,
turretClicks = (TextView)findViewById(R.id.turretClicks);
Run Code Online (Sandbox Code Playgroud)
只有在设置之后ContentView,您的活动才知道要解析哪个布局文件树以查找您指定的视图.
接下来,stringRange对Integer内部onClick事件进行强制转换并在内部转换后移动所有代码onClick以避免NumberFormatException,
此外,@Override在onClick方法之前添加注释.
| 归档时间: |
|
| 查看次数: |
128 次 |
| 最近记录: |