これはAndroidスタジオで生成された標準的なコードで、メソッドinitAccount()を使用してユーザーモデルを取得し、myAccount変数に書き込みます。私はスナックバーか何かを行うと、データが存在しているが、私は、ナビゲーションヘッダーにユーザーデータを書き込みたい場合は、エラーが表示されます。NavigationDrawerヘッダーにRetrofitレスポンスを設定します
java.lang.RuntimeException:活動 ComponentInfoを{開始できませんcom.vegevgsdsfa/com.sgsgsgdbsdg.activities.MainActivity}: java.lang.NullPointerException:仮想メソッド を呼び出そうとしました 'java.lang.String com.nfjg; lahjg; glsg.models.User.getNickName()'を nullオブジェクト参照
それのラインnick.setText(myAccount.getNickName());
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
ApiCaller userInfo;
User myAccount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initAccount();
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();
}
});
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.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
View headerView = navigationView.inflateHeaderView(R.layout.nav_header_main);
TextView nick = (TextView)headerView.findViewById(R.id.txt_nickname);
nick.setText(myAccount.getNickName());
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void initAccount() {
userInfo = Core.buildInterceptCaller();
Call<User> call = userInfo.showAccount();
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
if (response.isSuccessful()) {
myAccount = response.body();
} else
Snackbar.make(getCurrentFocus(), response.code(), Snackbar.LENGTH_LONG).show();
}
@Override
public void onFailure(Call<User> call, Throwable t) {
}
});
}
}
私はこれに新しいので、問題の内容を理解できません。清聴ありがとうございました。
これは、ビューの初期化をinitAccount()で行う必要があることを意味しますか? – Stansfield
はい。私は私の答えを更新しました。ニックネームテキストビューをグローバルにする。ビューのidを得るためにバターナイフを試すことができます。私はずっと簡単だろう。 findViewByIDの必要はありません。これは余分な提案です。 :D –