-2

I am aware there are many forms on StackOverflow about this very topic but none of the fixed worked for me.

When I run the code:

muteText.setText("Testing")

I get an error.

MainActivity.java

//Class Variables

Button startTime;
Button endTime;

TextView muteTime;
TextView unmuteTime;

int hour;
int minute;


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

    //Define Variables
    startTime = (Button)findViewById(R.id.startButton);
    endTime = (Button)findViewById(R.id.endTime);
    muteTime = (TextView)findViewById(R.id.muteText);
    Log.i("SchedMute", "muteTime: " + muteTime.getText().toString());
    unmuteTime = (TextView)findViewById(R.id.unmuteTime);



    //Button Listeners
    startTime.setOnClickListener(this);
    endTime.setOnClickListener(this);

    //--\\
    hour = 0;
    minute = 0;




}

public void onClick(View v){
    int id = v.getId();

    if(id == R.id.startButton){ //Selecting time 2 Mute phone
        showTimePickerDialog(v);
    }
}

public void setStartTime(){
    muteTime.setText("Testing Testing 123");
}

(Error occurs in last Method (setStartTime))

I am calling the method in another class by doing so:

MainActivity activity = new MainActivity();
activity.setStartTime();

I spend a good 30 minutes trying to figure this one out. Error:

07-29 12:29:44.034  19958-19958/myApp.com.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: myApp.com.app, PID: 19958
    java.lang.NullPointerException
            at myApp.com.app.MainActivity$TimePickerFragment.onTimeSet(MainActivity.java:96)
            at android.app.TimePickerDialog.tryNotifyTimeSet(TimePickerDialog.java:223)
            at android.app.TimePickerDialog.onClick(TimePickerDialog.java:173)
            at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:170)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5678)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)
Squonk
  • 48,735
  • 19
  • 103
  • 135
Cooper Scott
  • 651
  • 8
  • 18
  • 1
    Without disrespect your question shows you need to go back to the drawing board, you shouldn't be calling a setter of a TextView from outside the activity. – Raanan Jul 29 '15 at 17:37
  • Consider using `startActivityForResult()` or `SharedPreferences` – An SO User Jul 29 '15 at 17:37
  • 1
    possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – njzk2 Jul 29 '15 at 17:38
  • You probably want to pass the startTime to the Activity in the extra parameters, check this question: http://stackoverflow.com/questions/5265913/how-to-use-putextra-and-getextra-for-string-data and use startActivity to start an Activity. – Raanan Jul 29 '15 at 17:39
  • @njzk2 : Essentially no, it's not a duplicate. Even if the OP traced and identified the cause of the NPE he/she wouldn't necessarily understand the cause being the use of `new` to attempt to create a functioning `Activity`. – Squonk Jul 29 '15 at 17:46
  • 1
    @Squonk I consider it is, because the basic debugging has not been performed and this question explains how to. The OP has not even posted the line that causes the crash, and mis-identified another method for being the cause of the crash. As such, the `how do I solve it` part of the question is very relevant. – njzk2 Jul 29 '15 at 17:49
  • `muteText.setText("Testing")` gives you an error ... shouldn't it be `muteTime.setText("Testing")`? Because you really declared `TextView muteTime;`, and not `TextView muteText;` – Phantômaxx Jul 29 '15 at 19:13

3 Answers3

5
MainActivity activity = new MainActivity();

You can NOT correctly create an instance of an Activity by using new. Never attempt to do this, it will not give you the behaviour you expected.

If you need to call a method in an Activity from a Fragment that is attached to it, you should used the approved approach of using a 'callbacks' interface.

See the following for Communicating with Other Fragments - even though it is about communicating with other Fragments, it is also the way a Fragment should communicate with the Activity it is attached to.

Squonk
  • 48,735
  • 19
  • 103
  • 135
0

You call to setText before the Activity finished it creation. You need to call the function from the new Activity onCreate().

yshahak
  • 4,996
  • 1
  • 31
  • 37
0

You should never call the constructor of an Activity. If you do, none of its activity lifecycle will occur. I'm guessing you're doing that in the time picker dialog, you should make that activity be a listener (or pass in an anonymous inner class listener) to handle the set time.

Kyle Jahnke
  • 685
  • 3
  • 15