简体   繁体   中英

Java Error: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference

I get the error "Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference" when I want the start my app.

The error output

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);
}}

Please help.

I guess you have to replace the line

Intent sb_login = null;

with

Intent sb_login;

then try

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM