当我想要启动我的app时,我在空对象引用上得到错误“尝试调用虚方法'void android.widget.TextView.setText(java.lang.CharSequence)'。
错误输出
02-09 19:39:20.364 18846-18846/com.michael1011.tweetcomposer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.michael1011.tweetcomposer, PID: 18846
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.michael1011.tweetcomposer/com.michael1011.tweetcomposer.login}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2450)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.support.design.widget.Snackbar.setText(Snackbar.java:334)
at android.support.design.widget.Snackbar.make(Snackbar.java:210)
at com.michael1011.tweetcomposer.login.onCreate(login.java:43)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2403)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2520)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1363)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5466)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
mainActivity.java
public class login extends AppCompatActivity {
public boolean isOnline() {
NetworkInfo netInfo = ((ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
if (netInfo == null || !netInfo.isConnectedOrConnecting()) {
return false;
}
return true;
}
Intent sb_login = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar login_toolbar = (Toolbar) findViewById(R.id.toolbar_login);
setSupportActionBar(login_toolbar);
RelativeLayout login_sb_layout = (RelativeLayout)findViewById(R.id.login_layout);
Snackbar sb_login = Snackbar
.make(login_sb_layout, "@string/no_internet_connection", Snackbar.LENGTH_INDEFINITE)
.setAction("RETRY", new View.OnClickListener() {
@Override
public void onClick(View v) {
recreate();
}
});
sb_login.setActionTextColor(Color.parseColor("#55acee"));
View sb_login_view = sb_login.getView();
TextView sb_login_textView = (TextView)sb_login_view.findViewById(android.support.design.R.id.snackbar_text);
sb_login_textView.setTextColor(Color.parseColor("#ffffff"));
if(!isOnline()) {
sb_login.show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
startActivity(new Intent(this, settings.class));
return true;
}
return super.onOptionsItemSelected(item);
}}
请帮忙。
答案 0 :(得分:-2)
我猜你必须换行
Intent sb_login = null;
与
Intent sb_login;
然后尝试