开发者

EditText.settext from another class

Hi I am totally new to android, so this is a very basic question.

I want to change the text in a Edittext from another class but the program crashes every time.

My question is, how do i call newTxt() from another class (that extends homesc1 )?

This is my code.

public class homesc1 extends Activity {
    /** Called when the activity is first created. */
    public EditText clickEditText;
    int count =0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        clickEditText = (EditText)findViewById(R.id.editTextClick);
        newTxt("Works fine. call from same class");
    }

    public void newTxt(String txt) {
        clickEditText.setText(txt);
    }
}

public class SubC extends homesc1 {
 public void retur(){
  //clickEditText.setText("RETUR");
  newTxt("this crashes the program, no compile error");
 }
} 

// called is other class with:

SubC SC = new SubC();
SC.retur();

[EDIT] the Log Cat:

02-16 18:40:44.705: ERROR/AndroidRuntime(447): ERROR: thread attach failed
02-16 18:40:46.795: ERROR/AndroidRuntime(455): ERROR: thread attach failed
02-16 18:40:47.705: WARN/dalvikvm(462): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
02-16 18:40:47.727: ERROR/AndroidRuntime(462): Uncaught handler: thread main exiting due to uncaught exception
02-16 18:40:47.755: ERROR/AndroidRuntime(462): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.huskkage.android.but2ud/com.huskkage.android.but2ud.homesc1}: java.lang.NullPointerException
02-16 18:40:47.755: ERROR/AndroidRuntime(462):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
02-16 18:40:47.755: ERROR/AndroidRuntime(462):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
02-16 18:40:47.755: ERROR/AndroidRuntime(462):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-16 18:40:47.755: ERROR/AndroidRuntime(462):开发者_StackOverflow社区     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-16 18:40:47.755: ERROR/AndroidRuntime(462):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-16 18:40:47.755: ERROR/AndroidRuntime(462):     at android.os.Looper.loop(Looper.java:123)
02-16 18:40:47.755: ERROR/AndroidRuntime(462):     at android.app.ActivityThread.main(ActivityThread.java:4363)
02-16 18:40:47.755: ERROR/AndroidRuntime(462):     at java.lang.reflect.Method.invokeNative(Native Method)
02-16 18:40:47.755: ERROR/AndroidRuntime(462):     at java.lang.reflect.Method.invoke(Method.java:521)
02-16 18:40:47.755: ERROR/AndroidRuntime(462):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-16 18:40:47.755: ERROR/AndroidRuntime(462):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-16 18:40:47.755: ERROR/AndroidRuntime(462):     at dalvik.system.NativeStart.main(Native Method)
02-16 18:40:47.755: ERROR/AndroidRuntime(462): Caused by: java.lang.NullPointerException
02-16 18:40:47.755: ERROR/AndroidRuntime(462):     at android.app.Activity.findViewById(Activity.java:1612)
02-16 18:40:47.755: ERROR/AndroidRuntime(462):     at com.huskkage.android.but2ud.homesc1.newTxt(homesc1.java:67)
02-16 18:40:47.755: ERROR/AndroidRuntime(462):     at com.huskkage.android.but2ud.SubC.retur(SubC.java:10)
02-16 18:40:47.755: ERROR/AndroidRuntime(462):     at com.huskkage.android.but2ud.homesc1.onCreate(homesc1.java:56)
02-16 18:40:47.755: ERROR/AndroidRuntime(462):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-16 18:40:47.755: ERROR/AndroidRuntime(462):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
02-16 18:40:47.755: ERROR/AndroidRuntime(462):     ... 11 more
02-16 18:40:47.805: ERROR/dalvikvm(462): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
02-16 18:40:56.735: WARN/ActivityManager(63): Launch timeout has expired, giving up wake lock!
02-16 18:40:57.247: WARN/ActivityManager(63): Activity idle timeout for HistoryRecord{43d5ed00 com.huskkage.android.but2ud/.homesc1}


You might want to check your onCreate in your instance of SubC.

You do not appear to be calling super.onCreate, which means clickEditText will never be initialised and that is likely the source of the problem since it will be uninitialised when you call newTxt() from your subclass, likely causing a NullPointerException.

Just add

super.onCreate ( savedInstanceState );

to the beginning of your subclass onCreate, and that should fix the problem.


Creating a new class that extends your activity also ends up creating another activity ( that the system does not control at all ) that likely does not follow the proper init process, so nothing gets set up properly on your subclass.

Even if everything got set up properly worked, the subclass is not referring to your current activity instance, but its very own.

If you can guarantee the activity will be around for as long or longer as the other class will, you can pass the activity as part of the constructor to the second class, then call your method using that variable. ( Be aware that if you have extra references to your activity floating around, you will leak memory )


I use a Global class to store my activities when they're created. This way I can influence their views from other classes.

public static Activity gActivity = null;
public static Context gContext = null;

public void onCreate(Bundle savedInstanceSate){
  Global.gActivity = (Activity)this;
  Global.gContext = (Context)this;
}


Try something like this to your newTxt(s) method:

if (clickEditText == null) 
      clickEditText = (EditText)findViewById(R.id.editTextClick);
clickEditText.setText(s);

Or if your newText() method doesn't do anything beyond what you've shown here I would do away with it and just call findViewById() and setText() from both activities without making a seperate method.


You can't change TextView from Thread object, it doesn't have access to UI. It would take much time to explain, so You should find out it yourself.

Simpliest way is to use AsyncTask instead of Thread, and interact with UI using onPreExecute(), onPostExecute() & publishProgress() of AsyncTask.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜