0

How to solve this error when i want to call TextView in other method its show error Please help me this This is MainActivity Class i want show message from this method

  public class MainActivity extends AppCompatActivity {
  private final Context mContext = this;
private SignalRService mService;
private boolean mBound = false;
private TextView lblmessage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lblmessage = (TextView) findViewById(R.id.txtlbl);
    Intent intent = new Intent();
    intent.setClass(mContext, SignalRService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}

     public void showm(String v)
        {
            TextView  lblmessage2 = (TextView) findViewById(R.id.txtlbl2);
           lblmessage2.setText("Customer: "+ v);

        }

}

Error

  FATAL EXCEPTION: main
    Process: com.example.simplesignalrclient, PID: 31222
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)'
    on a null object reference
    at android.app.Activity.findViewById(Activity.java: 2093)
    at com.example.simplesignalrclient.MainActivity.showm(MainActivity.java: 91)
    at com.example.simplesignalrclient.MainActivity.showmessage(MainActivity.java: 87)
    at com.example.simplesignalrclient.SignalRService$1$1.run(SignalRService.java: 128)
    at android.os.Handler.handleCallback(Handler.java: 739)
    at android.os.Handler.dispatchMessage(Handler.java: 95)
    at android.os.Looper.loop(Looper.java: 160)
    at android.app.ActivityThread.main(ActivityThread.java: 5541)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java: 372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java: 964)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java: 759)
sharp
  • 1,191
  • 14
  • 39

1 Answers1

-1

For the Main Activity when calling the Method-

yourClass.showm(v,lblmessage2);

And in the class

 public void showm(String v,TextView lblmessage2)
    {

       lblmessage2.setText("Customer: "+ v);

    }
Daksh Agrawal
  • 855
  • 1
  • 11
  • 22