1

I've written a function to change the text of a TextView. It's in the class of the activity which contains the TextView. However, this function doesn't work when it's called from another class.

The function looks like this:

public class MainActivity extends AppCompatActivity {
    public void changeTest() {
        TextView test = (TextView) findViewById(R.id.textViewTest);
        guessedLetters.setText("test");
    }
}

If I call this function from my MainActivity, it works perfectly fine:

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gameplay);

        changeTest();
    }
}

But if I call it from a class that extends MainActivity, I get a java.lang.NullPointerException at android.app.Activity.findViewById(Activity.java:1884).

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gameplay);

        MyClass myClass = new MyClass();
        myClass.myFunction();
    }
}

public class MyClass extends MainActivity {
    public void myFunction() {
        MainActivity mainActivity = new MainActivity();
        mainActivity.changeTest();
    }
}

How could I fix this?

4
  • It works when in onCreate you have called setContentView but have you called it by the other way? can you post at least that line on both onCreate methods? Commented Nov 19, 2015 at 12:49
  • I've updated the code, hopefully it's clearer now :-) Commented Nov 19, 2015 at 13:25
  • @Algorithm_NL I posted some code that you can try to replicate Commented Nov 19, 2015 at 13:26
  • In MyClass you dont need to create object of MainActivity in that method just write this.changeTest(), I think it will change. Commented Nov 19, 2015 at 13:33

3 Answers 3

1

If you want this result using another activity extending from MainActivity, you can do it in this way:

MainActivity

public class MainActivity extends AppCompatActivity {

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

        //changeTest(this);

       AnotherActivity a = new AnotherActivity();
        a.updateTest(this);


    }

    public void changeTest(Activity activity) {
        TextView test = (TextView)activity.findViewById(R.id.textViewTest);
        test.setText("test");
    }
}

AnotherActivity

public class AnotherActivity extends MainActivity {

   public void updateTest(Activity activity){
       changeTest(activity);
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, this did the trick! However, I'm now having the same problem when defining a new Intent (instead of defining a TextView) with Intent intent = new Intent(this, GameplayActivity.class);. Where should I add 'activity' there?
where are you defining this intent? inside the method changeTest()?
1

Might this help you.

MainActivity

public class MainActivity extends AppCompatActivity {

public static TextView tv;

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

    tv = (TextView) findViewById(R.id.textView);
    OtherClass otherClass = new OtherClass();
    otherClass.setText("TEST");
    }
}

Other Class

public class OtherClass {
    public void setText(String txt){
       MainActivity.tv.setText(txt);
    }
}

Comments

1

Try with this

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gameplay);

        MyClass myClass = new MyClass();
        myClass.myFunction(this);
    }
}

public class MyClass extends MainActivity {
    public void myFunction(MainActivity mainActivity) {
        mainActivity.changeTest();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.