Question

Ok, so I have searched through a massive amount of other questions about the same thing but somehow none of those solutions have worked for my problem. So here is my problem, my Android-App crashes as soon as it has to get the text from an EditText to a String. I have my "Main"-Activity here:

package ign.test.fre;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView; 

public class testfre extends Activity {
    public static String cv = "";
    public static String cm = "";
    public static String cz = "";
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 

        //OTHER DEFINITIONS

    EditText ev = (EditText) findViewById(R.id.v);
    final Spinner em =(Spinner) findViewById(R.id.m);
    final Spinner ez = (Spinner) findViewById(R.id.z);
    final Button ee = (Button) findViewById(R.id.GO);

         //LOTS OF SPINNER STUFF

    ee.setOnClickListener(new AdapterView.OnClickListener() {
        public void onClick(View arg0) {
            if(initialsetup == false){
                enter = true;
                run();
            }
        }
    });
    ev.setOnKeyListener(new AdapterView.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            int key = event.getKeyCode();
            if(key == KeyEvent.KEYCODE_ENTER){
                enter = true;
            }
            return false;
        }
    });     
    initialsetup = false;
}
public void run(){
        if(enter == true){
        Calc.Test0();                               
        Calc.Test1_se();                            
        Calc.Test2_etre();                                  
        Calc.Test3_irr();                               
            if(Test3_irr == false){

        //MORE CODE

        textview6.setText("" + ausgabezeile[5]);      
        }   
    }
}

My Calc-Class looks like this: package ign.test.fre;

public class Calc {

   public static void Test0(){
      testfre.cv = testfre.ev.getText().toString();
      testfre.cm = Data.m()[testfre.em.getSelectedItemPosition()];
      testfre.cz = Data.zeiten()[testfre.ez.getSelectedItemPosition()];
      testfre.cv = testfre.cv.toLowerCase();
      testfre.per1.setText(Data.personen()[0]);
      //...
   }
   public static void Test1_se(){
     //MORE CODE

and it's in the first 3 lines of the Test0() that there's a problem. It dosen't matter which of those lines is first or where in the code they are it's always them. Even moving them to the run() or onClick() didn't change anything.

My xml-layout looks like this:

<android:weightSum="1">
<EditText android:text="Enter Text" android:id="@+id/v" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true"></EditText>

<Spinner android:layout_width="100dp" android:id="@+id/m" android:layout_height="wrap_content" android:layout_below="@+id/v" android:layout_alignParentLeft="true"></Spinner>
<Button android:id="@+id/GO" android:layout_width="70dp" android:layout_height="wrap_content" android:text="GO" android:layout_alignTop="@+id/z" android:layout_alignParentRight="true"></Button>
<Spinner android:layout_width="150dp" android:id="@+id/z" android:layout_height="wrap_content" android:layout_below="@+id/v" android:layout_toRightOf="@+id/m"></Spinner>

My Logcat-entry looks like this (ignian.lomoko.french is the same as ign.test.fre and so forth):

10-25 11:58:41.028: ERROR/AndroidRuntime(316):     at android.app.ActivityThread.main(ActivityThread.java:3683)
10-25 11:58:41.028: ERROR/AndroidRuntime(316):     at java.lang.reflect.Method.invokeNative(Native Method)
10-25 11:58:41.028: ERROR/AndroidRuntime(316):     at java.lang.reflect.Method.invoke(Method.java:507)
10-25 11:58:41.028: ERROR/AndroidRuntime(316):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-25 11:58:41.028: ERROR/AndroidRuntime(316):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-25 11:58:41.028: ERROR/AndroidRuntime(316):     at dalvik.system.NativeStart.main(Native Method)
10-25 11:58:41.038: WARN/ActivityManager(61):      Force finishing activity ignian.lomoko.french/.lomokofrench
10-25 11:58:41.558: WARN/ActivityManager(61):      Activity pause timeout for HistoryRecord{405c6018 ignian.lomoko.french/.lomokofrench}
10-25 11:58:43.698: INFO/Process(316):             Sending signal. PID: 316 SIG: 9
10-25 11:58:43.708: INFO/ActivityManager(61):      Process ignian.lomoko.french (pid 316) has died.
10-25 11:58:43.708: INFO/WindowManager(61):        WIN DEATH: Window{40691bd8 ignian.lomoko.french/ignian.lomoko.french.lomokofrench paused=false}
10-25 11:58:43.809: WARN/InputManagerService(61):  Got RemoteException sending setActive(false) notification to pid 316 uid 10034
10-25 11:58:52.691: WARN/ActivityManager(61):      Activity destroy timeout for HistoryRecord{405c6018 ignian.lomoko.french/.lomokofrench}

Thanks for any help upfront as I can seriously not find any kind of solution anywhere else. The problem as I understand it upto now with the help of others is that I can't access my variables from my "Calc"-Class. What I'm looking for I guess is a way to make the Spinners and EditText public.

Well I followed the solution from dten and now it works!

Was it helpful?

Solution

There are SO many problems with this code it's hard to know where to start

There are statics you then make duplicate variables for in a method but then try reference outside

You're referencing one static from your testfre (which should have a capitcal letter for a name) which you don't mean to be static but you seem to have set to be static because your Calc methods are static

you're referencing things from testfre in your calc class that dont'# even exist in testfre

it's one insane mess

why do you set a keylistener rathen than use on keydown

setting text on your edittext that is in your activity from a static class?

please give variables proper names not just 1 letter

what is Test3_irr and textview6?

... more to come

public class TestFre extends Activity {

    private EditText ev;
    private Spinner em;
    private Spinner ez;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ev = (EditText) findViewById(R.id.v);
        em = (Spinner) findViewById(R.id.m);
        ez = (Spinner) findViewById(R.id.z);
    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.GO:
            runCalculateAndSetText();
        }
    }

    private void runCalculateAndSetText() {

        String word = ev.getText().toString();
        int spinner1pos = em.getSelectedItemPosition();
        int spinner2pos = ez.getSelectedItemPosition();

        if ("".equals(word))
            return;
        if (spinner1pos < 0)
            return;
        if (spinner2pos < 0)
            return;

        // run evaluation for Je and populate textview
        String result = Calc.evaluateJe(word, spinner1pos, spinner2pos);
        ((TextView) findViewById(R.id.resultJe)).setText(result);

        // run evaluation for Tu and populate textview
        String result = Calc.evaluateJe(word, spinner1pos, spinner2pos);
        ((TextView) findViewById(R.id.resultTu)).setText(result);

        // run evaluation for Il Elle On and populate textview
        String result = Calc.evaluateJe(word, spinner1pos, spinner2pos);
        ((TextView) findViewById(R.id.resultIllElleOn)).setText(result);

        // Continue for rest of options

    }
}

please add

onClick="onClick" 

to your button in the XML (this will call onClick in the activity)

Your Calc class should be able to take the word you've typed and the select box index (you seem to be getting the value this relates to from somewhere else) and return you the string to fill the textview

The important thing is your Calc class should know NOTHING about your Activity

OTHER TIPS

One problem i see in your code is you are declaring the objects twice.

See in your activity you took

public static EditText ev;
public static Spinner em;
public static Spinner ez;   

and inside onCreate of Activity you are again doing the same thing :

EditText ev = (EditText) findViewById(R.id.v);
final Spinner em =(Spinner) findViewById(R.id.m);
final Spinner ez = (Spinner) findViewById(R.id.z);

Try replacing above code with below one in onCreate of testfre Activity :

ev = (EditText) findViewById(R.id.v);
em =(Spinner) findViewById(R.id.m);
ez = (Spinner) findViewById(R.id.z); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top