我想在创建活动时更改TextViews值。
在LoginActivity上,我使用以下命令开始活动:
startActivity(new Intent(LoginActivity.this, MainActivity.class));
但是我不知道在哪里放置用于更改textviews值的代码。
当我将其放入onCreate时,我的应用程序崩溃了...
我的logcat无法正常工作,所以我看不到我的异常...
但这样做时会崩溃
TextView t = (TextView) findViewById(R.id.zanuka_username);
t.setText("lllll");
onCreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t = (TextView) findViewById(R.id.zanuka_username);
t.setText("lllll");
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/** FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
**/
//ImageView profilePicture = (ImageView)findViewById(R.id.zanuka_profilepicture);
//new ImageLoadTask("http://chat.keecode.net/account/picture/1", profilePicture).execute();
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
最佳答案
您收到此异常:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
告诉您TextView t为NULL。其原因是您的activity_main.xml中缺少id =“ zanuka_username”的TextView。
关于android - 在创建 Activity 时更改TextView的值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35246719/