2016-02-09 111 views
0

我收到錯誤「嘗試調用虛方法」void android.widget.TextView .setText(java.lang.CharSequence)'在空對象引用上'當我想啓動我的應用程序時。Java錯誤:嘗試調用空對象引用虛擬方法'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

我沒有任何問題,運行代碼。你可以發佈你的activity_login.xml嗎? –

+0

可能重複[什麼是NullPointerException,以及如何解決它?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it ) – Soham

回答

-1

我想你有

Intent sb_login; 

更換線

Intent sb_login = null; 

然後嘗試

+1

這不是一個真正有用的答案。你需要添加解釋,爲什麼這會在解決問題方面產生任何影響。 – Adrian

相關問題