我想在创建活动时更改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);
}
答案 0 :(得分:1)
你得到了这个例外:
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。
答案 1 :(得分:0)
TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
mTextView = (TextView)findViewById(R.id.your_text_view);
mTextView.setText("Main activity created");
}
答案 2 :(得分:0)
请发布您的MainActivity oncreate代码和您获得的例外
这应该有效
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText("Hello World");
在你的onCreate中。