0

here is the code that i used:

        try {
        Toast.makeText(getApplicationContext(),
                "The application language now is English",
                Toast.LENGTH_LONG).show();

        setContentView(R.layout.activity_main);
        welcomeTextView = (TextView) findViewById(R.id.Welcome_Text_View);
        welcomeTextView.setText(R.string.WelcomeTextView_1);
        Album_Button = (Button) findViewById(R.id.Album_Button);
        About_us_Button = (Button) findViewById(R.id.About_Us_Button);
        Contact_us_Button = (Button) findViewById(R.id.Contact_Us_Button);
        Exit_Button = (Button) findViewById(R.id.Exit_Button);
        Contact_us_Text = (TextView) findViewById(R.id.contact_us_text_view);
        Album_Button.setText(R.string.AlbumButton_1);
        About_us_Button.setText(R.string.AboutUsButton_1);
        Contact_us_Button.setText(R.string.ContactUsButton_1);
        Exit_Button.setText(R.string.Exit_Text_1);

        Contact_us_Text = (TextView) findViewById(R.id.contact_us_text_view);
        about_us_Text = (TextView) findViewById(R.id.About_Us_Text_View);
        Contact_us_Text.setText(R.string.Contact_Us_Text_1);
        about_us_Text.setText(R.string.About_Us_Text_1);
    }

    catch (Exception ex) {
        Toast.makeText(getApplicationContext(), ex.toString(),
                Toast.LENGTH_LONG).show();
    }

what i am trying to do is to change the value of buttons text and textview text from another activity whenever i try to access these controls the compiler run into Null Pointer exception... can any one help me?

Dexter90
  • 69
  • 2
  • 14
  • Why didn't you post the stacktrace – EpicPandaForce Aug 26 '14 at 13:17
  • @Zhuinden actually i am new to android development so can you tell me what is stack trace and how to put it here? – Dexter90 Aug 26 '14 at 13:19
  • 1
    You can not access `View`s from other `Activity` in such a manner. – Apoorv Aug 26 '14 at 13:19
  • can you help me to do it actually i am trying to do a application with three language so whenever the user click on a language i change the string value of a control but i stuck with this error if there is any solution please tell me and if you have a link for that solution i will be thankful. – Dexter90 Aug 26 '14 at 13:21
  • use this link http://stackoverflow.com/questions/3280051/how-to-enable-logcat-console-in-eclipse-for-android – nobalG Aug 26 '14 at 13:22
  • Stack Trace is basically the output of the Logcat. If it's not visible by default, and you are using Eclipse ADT, then it's in the DDMS perspective. It's basically what says you have a null pointer exception on what line. Also, Activities can only access their own content views. – EpicPandaForce Aug 26 '14 at 13:22
  • 08-26 16:23:29.978: I/ViewRootImpl(31517): ViewRoot's Touch Event : ACTION_DOWN 08-26 16:23:30.078: I/ViewRootImpl(31517): ViewRoot's Touch Event : ACTION_UP 08-26 16:23:30.608: I/ViewRootImpl(31517): ViewRoot's Touch Event : ACTION_DOWN 08-26 16:23:30.698: I/ViewRootImpl(31517): ViewRoot's Touch Event : ACTION_UP 08-26 16:23:30.758: E/ViewRootImpl(31517): mIsPenSupport: :false 08-26 16:23:31.688: I/ViewRootImpl(31517): ViewRoot's Touch Event : ACTION_DOWN 08-26 16:23:31.748: I/ViewRootImpl(31517): ViewRoot's Touch Event : ACTION_UP – Dexter90 Aug 26 '14 at 13:25
  • 08-26 16:23:31.938: E/ViewRootImpl(31517): mIsPenSupport: :false – Dexter90 Aug 26 '14 at 13:26
  • i just mentioned the logCat and i updated the code used so if you can recheck the code again – Dexter90 Aug 26 '14 at 13:28
  • what is happen is the compiler could not detect the controls from other activity at the run time because at the contact_us_textView after the Exit_ Button the contact_us_textView still null so when he try to change the string value he run into null pointer exception – Dexter90 Aug 26 '14 at 13:32
  • @Apoorv would you have any solution ?? – Dexter90 Aug 26 '14 at 13:35
  • those are just events and not relevant in any way to the exception. You must post the exception stacktrace from the logcat. – EpicPandaForce Aug 26 '14 at 13:40

3 Answers3

1

If you want to support different languages in android app. You should define different resources like following.

  • res/values/strings.xml
  • res/values_it/strings.xml
  • res/values_ja/strings.xml
  • ...

There is no need to change language at run time.

http://developer.android.com/training/basics/supporting-devices/languages.html

0

Are you using the layout setContentView(R.layout.activity_main); in other activity?...

you must have to use the correct layout.

If you want get the text from the editText, use the same activity ant send the values from an Intent (putExtra)

EditText edit =  (EditText) findViewById(R.id.text_xyz);
String value =  edit.getText.tostring();

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("some_key", value);
intent.putExtra("some_other_key", "a value");
startActivity(intent);

Secont Activity:

 Bundle extras = intent.getExtras();
    if(extras != null)
    String data = extras.getString("keyName"); // retrieve the data using keyName 
Gilberto Ibarra
  • 2,849
  • 2
  • 27
  • 38
  • what do you mean can you explain to me more if i use setContentView(R.layout.2ndactivity) then when i run the app it go to the second screen – Dexter90 Aug 27 '14 at 06:53
  • When your app start setContentView(R.layout.activity_main); load de XML activityMain and only can use the ids defined in the XML. if your Secont Activity use other XML file like setContentView(R.layout.2ndactivity); You can only use the ids defined in 2ndactivity.xml . – Gilberto Ibarra Aug 27 '14 at 16:45
  • lbarra whenever i used the put extra method whenever you press the back button in the 2nd activity and renter the activity then the textView will be null then nothing will be displayed ... so any other soltuion and can you explain to me the comment that you did it ? – Dexter90 Aug 28 '14 at 07:34
0

You cannot access view of one activity in different activity. If you want to you need to set the same layout and than create the objects and use it as

Album_Button = (Button) findViewById(R.id.Album_Button);//this returns View Object
Opal
  • 81,889
  • 28
  • 189
  • 210
Rohit
  • 191
  • 2
  • 9