[英]EditText.getText().toString() crashes
In my android application , I have 3 dialogue boxes in which the user puts info into 3 editTexts and it will display the one of the data onto another class/page after it randomly picks which data to choose. 在我的android应用程序中,我有3个对话框,用户在其中将信息放入3个editTexts中,并在它随机选择要选择的数据后,将其中一个数据显示到另一个类/页面上。
This is my mainClass 这是我的主班
public class MainActivity extends Activity {
//Variables are displayed in this area
String choices[] = new String[3];
//EditText editText;
//EditText editText2;
//EditText editText3;
Button mainButton ;
Random rand = new Random();
int finalChoice;
String displayChoice = "";
EditText editText ;
EditText editText2;
EditText editText3;
EditText editText4;
String test;
int count = 3;
//--------------------------- --------------------------------------------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Makes Button
setContentView(R.layout.activity_main);
declareTextBox();
setButton();
String choice1 = editText.getText().toString();
String choice2 = editText2.getText().toString();
String choice3 = editText3.getText().toString();
choices[0] = choice1; //pass from click button to method.
choices[1] = choice2;
choices[2] = choice3;
finalChoice =rand.nextInt(2);
}
public void setButton()
{
final Button mainbutton = (Button) findViewById(R.id.mainButton);
mainbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
test = ((EditText) findViewById(R.id.editText4)).getText().toString();
// count++;
//retChoice();
// loadScreen();
Intent i = new Intent(MainActivity.this, resultScreen.class);
i.putExtra("display" , displayChoice);
Intent intent = new Intent(MainActivity.this,loadingScreen.class);
//start the second Activity
MainActivity.this.startActivity(intent);
}
});
}
public void declareTextBox()
{
editText = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText3);
}
public void getString(String finalChoice)
{
finalChoice = displayChoice;
}
public String retChoice()
{
displayChoice = choices[finalChoice];
return displayChoice;
}
public void clearText()
{
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
editText.setText(" ");
}
});
editText2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
editText2.setText(" ");
}
});
editText3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
editText3.setText(" ");
}
});
}
public void getText2()
{
}
public void getText()
{
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
This is my Display class : 这是我的Display课:
public class resultScreen extends Activity {
MainActivity ma = new MainActivity();
//Method supposedly retrieves the string data from MainActivity Class but somehow displayed null instead.
//Find a way to keep the string variable when transfering from one class to another class.
String finalResult = ma.retChoice();
public void onCreate(Bundle resultScreen){
super.onCreate(resultScreen);
setContentView(R.layout.resultscreen);
//ma.displayChoice.toString();
String str = finalResult;
TextView text = (TextView) findViewById(R.id.textView1);
text.setText(str);
final Button backbutton = (Button) findViewById(R.id.backButton);
backbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
Intent intent = new Intent(resultScreen.this,MainActivity.class);
resultScreen.this.startActivity(intent);
}
});
}
}
And this is my loading screen class right after the main button is clicked 这是我单击主按钮后的加载屏幕课程
public class loadingScreen extends Activity{
protected void onCreate(Bundle loadingScreen) {
// TODO Auto-generated method stub
super.onCreate(loadingScreen);
setContentView(R.layout.loadingscreen);
//If sound clip 20 sec long we don't want to carryit outside next class
// A timer thread looking for "run" method
Thread timer = new Thread()
{
public void run() {
try {
//this is how many mil sec
sleep(8000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent intent = new Intent(loadingScreen.this, resultScreen.class);
loadingScreen.this.startActivity(intent);
loadingScreen.this.finish();
}
}
};
timer.start();
}
} }
My app crashes a few seconds into the loading screen when the program attempts to display the data on the resultScreen. 当程序尝试在resultScreen上显示数据时,我的应用程序在加载屏幕上崩溃了几秒钟。
Here's my logCat 这是我的logCat
05-17 22:32:54.446: D/AndroidRuntime(1181): Shutting down VM
05-17 22:32:54.446: W/dalvikvm(1181): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
05-17 22:32:54.636: E/AndroidRuntime(1181): FATAL EXCEPTION: main
05-17 22:32:54.636: E/AndroidRuntime(1181): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.choiceprototest/com.example.choiceprototest.resultScreen}: java.lang.NullPointerException
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.os.Handler.dispatchMessage(Handler.java:99)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.os.Looper.loop(Looper.java:137)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.ActivityThread.main(ActivityThread.java:5039)
05-17 22:32:54.636: E/AndroidRuntime(1181): at java.lang.reflect.Method.invokeNative(Native Method)
05-17 22:32:54.636: E/AndroidRuntime(1181): at java.lang.reflect.Method.invoke(Method.java:511)
05-17 22:32:54.636: E/AndroidRuntime(1181): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-17 22:32:54.636: E/AndroidRuntime(1181): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-17 22:32:54.636: E/AndroidRuntime(1181): at dalvik.system.NativeStart.main(Native Method)
05-17 22:32:54.636: E/AndroidRuntime(1181): Caused by: java.lang.NullPointerException
05-17 22:32:54.636: E/AndroidRuntime(1181): at com.example.choiceprototest.MainActivity.retChoice(MainActivity.java:101)
05-17 22:32:54.636: E/AndroidRuntime(1181): at com.example.choiceprototest.resultScreen.<init>(resultScreen.java:18)
05-17 22:32:54.636: E/AndroidRuntime(1181): at java.lang.Class.newInstanceImpl(Native Method)
05-17 22:32:54.636: E/AndroidRuntime(1181): at java.lang.Class.newInstance(Class.java:1319)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
05-17 22:32:54.636: E/AndroidRuntime(1181): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
05-17 22:32:54.636: E/AndroidRuntime(1181): ... 11 more
05-17 22:33:03.385: I/Process(1181): Sending signal. PID: 1181 SIG: 9
05-17 22:33:05.435: E/Trace(1204): error opening trace file: No such file or directory (2)
Thanks guys. 多谢你们。
I believe I see the problem. 我相信我看到了问题。 You are trying to access your Array
but it is an instance variable instead of static so it is being killed off when you exit your Activity
. 您正在尝试访问Array
但它是一个实例变量而不是静态变量,因此在退出Activity
时将被杀死。 You can either put them in a static class and access them that way or pass a String
variable through your Intent
, depending on what you need. 您可以将它们放在静态类中并以这种方式访问它们,也可以根据需要通过Intent
传递String
变量。
If you have the value of the String
you need when you leave MainActivity
then I would probably just pass it through your Intent
. 如果您拥有离开MainActivity
时所需的String
值,那么我可能会通过您的Intent
传递它。 If the value depended on something in one of your other Activities
then I would consider putting it in a separate class but it doesn't look like that's what you need here 如果值取决于您其他“ Activities
某项,那么我会考虑将其放在单独的类中,但看起来好像不是您所需要的
Edit 编辑
You are already doing it here, kind of 你已经在这里做,有点
Intent i = new Intent(MainActivity.this, resultScreen.class);
i.putExtra("display" , displayChoice);
Intent intent = new Intent(MainActivity.this,loadingScreen.class);
But you aren't starting the Activity
that would get the String
但是您不会启动将获取String
的Activity
i.putExtra("display" , displayChoice);
that line would pass a String
with key "displpay"
and value of displayChoice
, although I think that value is an empty String
at that point, but you don't start that Activity
. 该行会通过一个String
与key "displpay"
和价值displayChoice
,虽然觉得值是一个空String
,在这一点上,但你不启动该Activity
。 I'm lost on how your flow is suppose to work but basically you would do something like this 我不知道您的流程应该如何运作,但是基本上您会做这样的事情
String displayChoice = "testValue"; // some arbitrary value--whatever you need to send
Intent i = new Intent(MainActivity.this, resultScreen.class); // create the Intent
i.putExtra("display" , displayChoice); // add the extra
startActivity(i); //start the Activity
then to get the value inside resultScreen
in onCreate()
然后在onCreate()
resultScreen
中获取值
Intent recIntent = getIntent(); // get the Intent that started this Activity
String value = recIntent.getStringExtra("display"); // get the value using the key
// value now equals "testValue"
I think your app chash because you have an ANR: "Application Not Responding" because you are running a long procces inside the UIThread. 我认为您的应用程序非常抢手,因为您有一个ANR:“应用程序无响应”,因为您在UIThread中运行了很长的过程。 (onCreate() method is form the UIThread) (onCreate()方法由UIThread组成)
Use rather as an Asyntack for your sleeping thread or one handler (with messages). 可以将它用作您的睡眠线程的Asyntack或一个处理程序(带有消息)。
If you need more I can edit your code tomorrow. 如果您需要更多,我明天可以编辑您的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.