1

So i got a project with the following activities : MainActivity/GetJson/ TimerActivity.

GetJson activity :

public class GetJson extends AppCompatActivity     {
    String JSON_STRING;
    String json;

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



    public void getJSON(View view){
        new BackgroundTask().execute();
    }


    public class BackgroundTask  extends AsyncTask<Void,Void,String>  {
        String json_url;



        @Override
        protected void onPreExecute() {
            json_url="http://10.10.103.36/projet/php/fichier.php";

        }

        @Override
        protected String doInBackground(Void... params) {
            try {
                URL url=new URL(json_url);
                HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
                InputStream inputStream=httpURLConnection.getInputStream();
                BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                StringBuilder stringBuilder=new StringBuilder();
                while  ((JSON_STRING= bufferedReader.readLine())!=null){
                    stringBuilder.append(JSON_STRING+"\n");
                }

                bufferedReader.close();
                inputStream.close();;
                httpURLConnection.disconnect();
                return stringBuilder.toString().trim();

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
        }

        @Override
        protected void onPostExecute(String result) {

            json=result;

        }



    }
}

Timer Activity

public class TimerActivity extends Activity {

    private TextView test;
    String msg = "Hey";

    private Handler mHandler = new Handler();





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        test = (TextView) findViewById(R.id.compteur);
        Timer timer = new Timer();
        TimerTask tt = new TimerTask()
        {
            @Override
            public void run()
            {
                test.setText(msg); 
            }
        };

        timer.scheduleAtFixedRate(tt,5000,1000);  // Delay 5 seconds on the first run
        // then run every second
        test.setText(msg);


        setContentView(R.layout.activity_main);
    }


}

In my xml main activity i got 2 textview : - compteur : to display a text from my timeractivity - textViewJson : to display my json

I think my methods to get json( from GetJson) and display text(from TimerActivity) are correct. But the problem is that i can't setText from others activities to my main activity. I don't have any compilation problem bu my textView aren't getting updated. I tried both in GetJson and TimerActivity to just do :

TextView test;
test = (TextView) findViewById(R.id.compteur);
test.setText(msg);

In order to check if i can change the textview text without even using the returned values and nothing happens.

Any ideas ?

Have a good day !

AlexMel
  • 21
  • 7
  • please clearly mention what your problem and what you want – android_jain Feb 14 '17 at 08:16
  • I want to display my json result in my MainActivity Layout in textviews, and change text of a textview each 5s. The problem as i tried to explain(My english isnt that good), is that i can't change text of textview from other activities. Ex: Change my TextViewJson (in mainActivity layout) from my GetJson activity – AlexMel Feb 14 '17 at 08:19
  • ok so you have json parsh data means which data you want to set in text view is comming??? – android_jain Feb 14 '17 at 08:22
  • or where you r facing problem it is big topic – android_jain Feb 14 '17 at 08:23
  • 1
    You shouldn't change the text of an activity from another activity because it can create a lot of issues (the most notable one is when the first activity is destroyed and you try to access it). To solve your issue, learn how you can communicate properly between activities http://stackoverflow.com/documentation/android/103/intent/533/getting-a-result-from-another-activity#t=20170214082209780327 – Iulian Popescu Feb 14 '17 at 08:25
  • i get data from my GetJson activity, and i want to display this data to a textview with ID: TextViewJson which is in a activity_main.xml – AlexMel Feb 14 '17 at 08:26
  • dont create Activty for json parse or do in background create simple java class or inner Asyncktask class and call – android_jain Feb 14 '17 at 08:27
  • #lulian Popescu Im going to read your link, thx for trying helping me – AlexMel Feb 14 '17 at 08:27
  • onother activity if you call there is saparete mathod for activity and ui – android_jain Feb 14 '17 at 08:28
  • create simple public java class without extending Activity – android_jain Feb 14 '17 at 08:28
  • in you activivity make object of that class call and use – android_jain Feb 14 '17 at 08:29
  • 1
    Possible duplicate of [How to update a TextView of an activity from another class](http://stackoverflow.com/questions/10996479/how-to-update-a-textview-of-an-activity-from-another-class) – Istiak Morsalin Feb 14 '17 at 08:33
  • possible duplicate of http://stackoverflow.com/questions/6605588/changing-text-from-another-activity – Istiak Morsalin Feb 14 '17 at 08:34

1 Answers1

1

Once you have the information you want to show in your TVs you should save it somewhere and load it when your Activity is created. You can't change the state of Views in a destroyed Activity. Use Intents (putExtra();) to pass data between your Activies or use SharedPreferences

T.Dimitrov
  • 157
  • 2
  • 7