Search code examples
androidactiverecordtabsandroid-tabhost

How to do Changes to Edit text field which is in the main activity, from the activity loaded inside the tab host which is in the main activity?


I am developing an app, where in my main activity, there is a tab host and a edit text field. Tab host will load different activities inside each tab when changing tabs. In each activity that loads in each tab I have placed a button. When I click the button I need some text to be displayed in the Edit text in the main activity.But I am getting "app forced closed" whenever i try to initialize the edit text field in the other activities than the main activity.

So if anybody can help me get the edit text get value from the press from the button in the activity loaded in the tab it would be a great help

This is the screen shot blue box will be the activity that loads in every tab, out of it will be the main activity

Screen shot of my app

This is what Logcat out put looks like when the force close occurs.

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.batz.voiceforyou/com.batz.voiceforyou.Things}: java.lang.NullPointerException
03-24 17:55:23.928: E/AndroidRuntime(23581):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2065)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:1906)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:694)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at android.widget.TabHost.setCurrentTab(TabHost.java:358)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:150)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:553)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at android.view.View.performClick(View.java:4129)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at android.view.View$PerformClick.run(View.java:17143)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at android.os.Handler.handleCallback(Handler.java:615)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at android.os.Looper.loop(Looper.java:137)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at android.app.ActivityThread.main(ActivityThread.java:4802)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at java.lang.reflect.Method.invokeNative(Native Method)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at java.lang.reflect.Method.invoke(Method.java:511)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:813)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:580)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at dalvik.system.NativeStart.main(Native Method)
03-24 17:55:23.928: E/AndroidRuntime(23581): Caused by: java.lang.NullPointerException
03-24 17:55:23.928: E/AndroidRuntime(23581):    at com.batz.voiceforyou.Things.onCreate(Things.java:19)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at android.app.Activity.performCreate(Activity.java:5013)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-24 17:55:23.928: E/AndroidRuntime(23581):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2029)
03-24 17:55:23.928: E/AndroidRuntime(23581):    ... 18 more  

Here is the MainActivity Code

public class MainActivity extends TabActivity {

String text="";

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




    final EditText mainText=(EditText) findViewById(R.id.maintext);


    Resources res =  getResources();
    TabHost tabs =  getTabHost();

    Intent regular =  new Intent(this, Regular.class);
    TabSpec reg =  tabs.newTabSpec("Regular")
            .setIndicator("", res.getDrawable(R.drawable.ic_launcher))
            .setContent(regular);

    Intent things =  new Intent(this, Things.class);
    TabSpec thngs =  tabs.newTabSpec("Things")
            .setIndicator("", res.getDrawable(R.drawable.ic_launcher))
            .setContent(things);

    tabs.addTab(reg);
    tabs.addTab(thngs);

    tabs.setCurrentTab(0);

}


}

This is one of the activity that loads inside the tab

public class Regular extends Activity {
String text="";
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.regular);


    //final EditText mainText=(EditText) findViewById(R.id.maintext);
    //text=mainText.getText().toString();

    Button regbtn =  (Button) findViewById(R.id.regularbutton);
    regbtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            text=text+" regular";
            //mainText.setText(text);
        }
    });

}

}

Here you can see the 3 line which I have commented, If I remove those lines the app will force close when starting. If you want I can share the layouts too


Solution

  • You can achieve this with your existing setup by sending a broadcast.

    First, create a BroadcastReceiver in your MainActivity like this:

    public static final String ACTION_UPDATE_TEXT = "com.batz.voiceforyou.UPDATE_TEXT";
    public static final String EXTRA_TEXT = "text";
    
    private BroadcastReceiver textReceiver = new  BroadcastReceiver() {
    
        @Override
        public void onReceive(Context context, final Intent intent) {
            if (intent != null && ACTION_UPDATE_TEXT.equals(intent.getAction())) {
                runOnUiThread(new Runnable() {
    
                    @Override
                    public void run() {
                        String text = intent.getStringExtra(EXTRA_TEXT);
                        EditText mainText=(EditText) findViewById(R.id.maintext);
                        mainText.setText(text);
                    }
                });
            }
        }
    };
    

    Then register the receiver in the onResume() method of the MainActivity:

    registerReceiver(textReceiver, new IntentFilter(ACTION_UPDATE_TEXT));
    

    Don't forget to unregister it in the onPause() method:

    unregisterReceiver(textReceiver);
    

    Next, in each of your Activities (Regular and Things) button onclick handler, send the broadcast like this:

        @Override
        public void onClick(View v) {
            //your other code
    
            Intent intent = new Intent(MainActivity.ACTION_UPDATE_TEXT);
            intent.putExtra(MainActivity.EXTRA_TEXT, text);
            sendBroadcast(intent);
        }