EditText.getText()。的toString()崩溃(EditText.getText(

2019-09-04 03:40发布

在我的Android应用程序,我在其中用户将信息分为3个editTexts 3个对话框和它会显示数据的一个到另一个类/页后,随机挑选,选择哪些数据。

这是我的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;
    }
}

这是我的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);     


      }
        });



        }

}

这是我的加载屏幕右侧类单击主按钮后

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();
}

}

我的应用程序崩溃,几秒钟到加载屏幕当程序试图以显示在resultScreen数据。

这里是我的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)

多谢你们。

Answer 1:

我相信我看到的问题。 您试图访问Array ,但它是一个实例变量,而不是静态的,所以它是被杀死了,当您退出Activity 。 您可以把它们放在一个静态类和访问他们的方式,或通过String通过您的变量Intent ,这取决于你所需要的。

如果你有值String时需要您离开MainActivity那么我很可能只是通过它通过你的Intent 。 如果该值在其他的一个依赖于一些Activities ,那么我会考虑把它放在一个单独的类,但它不像这就是你在这里需要什么

编辑

你已经做在这里,种

Intent i  = new Intent(MainActivity.this, resultScreen.class);
     i.putExtra("display" , displayChoice);

     Intent intent = new Intent(MainActivity.this,loadingScreen.class);

但是,你是不是开始的Activity ,将得到String

i.putExtra("display" , displayChoice);

该行会通过一个Stringkey "displpay"和价值displayChoice ,虽然觉得值是一个空String ,在这一点上,但你不启动该Activity 。 我失去了你的流量是如何想工作,但基本上你会做这样的事

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

然后进去值resultScreenonCreate()

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"


Answer 2:

我想你的应用程序chash因为你有一个ANR:“应用程序不响应”,因为你正在运行的UIThread内长procces。 (onCreate()方法是形成UIThread)

使用,而作为你的睡眠线程或一个处理器(带有信息)的Asyntack。

如果你需要更多的,我可以编辑代码的明天。



文章来源: EditText.getText().toString() crashes