当我想在其他方法中调用TextView时如何解决此错误显示错误请帮帮我 这是MainActivity Class我想要从这个方法显示消息
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);
}
}
错误
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)
答案 0 :(得分:-1)
调用方法时的主要活动 -
yourClass.showm(v,lblmessage2);
在课堂上
public void showm(String v,TextView lblmessage2)
{
lblmessage2.setText("Customer: "+ v);
}