将Retrofit响应设置为NavigationDrawer标头

时间:2017-09-03 09:52:09

标签: java android navigation-drawer retrofit2

这是Android Studio生成的标准代码,我的方法是initAccount(),我获取用户模型并将其写入myAccount变量。当我使用Snackbar或其他东西时,数据存在,但是当我想将用户数据写入导航标题时,会出现错误:

  

java.lang.RuntimeException:无法启动活动   ComponentInfo {com.vegevgsdsfa / com.sgsgsgdbsdg.activities.MainActivity}:   java.lang.NullPointerException:尝试调用虚方法   'java.lang.String com.nfjg; lahjg; glsg.models.User.getNickName()'on a   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) {

        }
    });
}

}

我是新手,我无法理解问题所在。谢谢你的关注。

1 个答案:

答案 0 :(得分:1)

在调用onResponse of refofit后,应在响应到达后设置昵称。但是现在你在onCreate中设置昵称意味着改造还没有完成它的工作,但是你试图设置昵称。所以它在setnickname中返回null。

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();
                nickname.setText(myAccount.getNickName); //Nickname global
            } else
                Snackbar.make(getCurrentFocus(), response.code(), Snackbar.LENGTH_LONG).show();
        }

        @Override
        public void onFailure(Call<User> call, Throwable t) {

        }
    });
}