所以我正在开发一个可以进行大量计算的个性化应用程序。 当我初始化应用程序崩溃的元素,如果我没有初始化任何元素,应用程序显示布局就好了,并做了一些基本功能,我添加了在XML中使用onClick属性的方法
我需要从EditText元素中提取数据以进行计算。
java代码:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//setting up the elements
EditText finInv = (EditText)findViewById(R.id.finInv);
String finInvString= finInv.getText().toString();
int investment=Integer.parseInt(finInvString);
Button button14=(Button)findViewById(R.id.button14);
EditText rawBroRate=((EditText) findViewById(R.id.bronzeRate));
EditText rawSilRate=((EditText) findViewById(R.id.silverRate));
EditText rawGoldRate=((EditText) findViewById(R.id.goldRate));
EditText rawEliteRate=((EditText) findViewById(R.id.eliteRate));
//there's more but I skipped it
}
崩溃的LOGCAT:
FATAL EXCEPTION:main进程:com.ajaydubey.fifamobilebulktradeprofitcounter,PID:17627 java.lang.RuntimeException:无法启动活动ComponentInfo {com.ajaydubey.fifamobilebulktradeprofitcounter / com.ajaydubey.fifamobilebulktradeprofitcounter.MainActivity}:java.lang.NumberFormatException :无效的int:“”在Android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2423)android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)android.app.ActivityThread.access $ 900(ActivityThread.java: 153)在Android.app.Hoper.loop(Looper.java:148)上的android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1349)在android.os.Handler.dispatchMessage(Handler.java:102)处android.app.ActivityThread.main(ActivityThread.java:5438)at java.lang.reflect.Method.invoke(Native Method)at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:738)at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)引起:java.lang.NumberForma tException:java.lang.Integer.parseInt(Integer.java:)中java.lang.Integer.parseInt(Integer.java:358)的java.lang.Integer.invalidInt(Integer.java:138)中的int:“”无效。 334)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)的android.app.Activity.performCreate(Activity.java:6303)的com.ajaydubey.fifamobilebulktradeprofitcounter.MainActivity.onCreate(MainActivity.java:19)中android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2376)在Android.app上的android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2483)android.app.ActivityThread.access $ 900(ActivityThread.java:153)。 ActivityThread $ H.handleMessage(ActivityThread.java:1349)位于android.app.AooT.Thread.main的android.os.Handler.dispatchMessage(Handler.java:102)android.os.Looper.loop(Looper.java:148) (ActivityThread.java:5438)位于com.android.internal.os.ZygoteInit的$ java.lang.reflect.Method.invoke(Native Method)$ MethodAndArgsCaller.run(ZygoteInit.java:738)a t com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
我是新来的机器人赦免任何愚蠢的错误
答案 0 :(得分:0)
在像on This这样的onCreate方法中初始化你的视图并执行空输入检查finInvString:
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setting up the elements
EditText finInv = (EditText)findViewById(R.id.finInv);
String finInvString= finInv.getText().toString();
if(!TextUtils.isEmpty(finInvString){ // Do make sure to set android:inputType="number" in your XML layout file.
int investment=Integer.parseInt(finInvString);
}
Button button14=(Button)findViewById(R.id.button14);
EditText rawBroRate=((EditText) findViewById(R.id.bronzeRate));
EditText rawSilRate=((EditText) findViewById(R.id.silverRate));
EditText rawGoldRate=((EditText) findViewById(R.id.goldRate));
EditText rawEliteRate=((EditText) findViewById(R.id.eliteRate));
}
}
**更新**
在您的布局文件中将此标记添加到ID为finInv:
的EditText元素
android:inputType="number"
答案 1 :(得分:0)
您必须将其放在onCreate()
方法内而不是外部,因为在初始化过程中您没有使用setContentView(..)
设置视图,例如像这样修复它
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText finInv = (EditText) findViewById(R.id.finInv);
String finInvString = finInv.getText().toString();
int investment = Integer.parseInt(finInvString);
Button button14 = (Button) findViewById(R.id.button14);
EditText rawBroRate = ((EditText) findViewById(R.id.bronzeRate));
EditText rawSilRate = ((EditText) findViewById(R.id.silverRate));
EditText rawGoldRate = ((EditText) findViewById(R.id.goldRate));
EditText rawEliteRate = ((EditText) findViewById(R.id.eliteRate));
}
答案 2 :(得分:0)
你的初始化应该在onCreate方法中。 请看一下这段代码
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setting up the elements
EditText finInv = (EditText)findViewById(R.id.finInv);
String finInvString= finInv.getText().toString();
int investment=Integer.parseInt(finInvString);
Button button14=(Button)findViewById(R.id.button14);
EditText rawBroRate=((EditText) findViewById(R.id.bronzeRate));
EditText rawSilRate=((EditText) findViewById(R.id.silverRate));
EditText rawGoldRate=((EditText) findViewById(R.id.goldRate));
EditText rawEliteRate=((EditText) findViewById(R.id.eliteRate));
}
}
答案 3 :(得分:0)
我认为你的应用程序在行中遇到了NullPointerException崩溃
String finInvString= finInv.getText().toString();
因为即使在调用'setContentView'之前也会引用finInv。检查'adb logcat'输出。
尝试,
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
// Member variables
EditText finInv;
Button button14;
EditText rawBroRate;
EditText rawSilRate;
EditText rawGoldRate;
EditText rawEliteRate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//setting up the elements
finInv = (EditText)findViewById(R.id.finInv);
String finInvString= finInv.getText().toString();
int investment=Integer.parseInt(finInvString);
button14=(Button)findViewById(R.id.button14);
rawBroRate=((EditText) findViewById(R.id.bronzeRate));
rawSilRate=((EditText) findViewById(R.id.silverRate));
rawGoldRate=((EditText) findViewById(R.id.goldRate));
rawEliteRate=((EditText) findViewById(R.id.eliteRate));
}
}
答案 4 :(得分:0)
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.*;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myinitialization();
}
public void myinitialization(){
//setting up the elements
EditText finInv = (EditText)findViewById(R.id.finInv);
String finInvString= finInv.getText().toString();
int investment=Integer.parseInt(finInvString);
Button button14=(Button)findViewById(R.id.button14);
EditText rawBroRate=((EditText) findViewById(R.id.bronzeRate));
EditText rawSilRate=((EditText) findViewById(R.id.silverRate));
EditText rawGoldRate=((EditText) findViewById(R.id.goldRate));
EditText rawEliteRate=((EditText) findViewById(R.id.eliteRate));
//there's more but I skipped it
}
}