2017-09-03 95 views
-1

这是Android Studio生成的标准代码,使用我的方法initAccount(),在那里获取用户模型并将其写入myAccount变量。当我做一个小吃吧什么的,数据是存在的,但我想给用户的数据写入到导航标题,会出现一个错误:将Retrofit响应设置为NavigationDrawer标头

了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 空对象引用

它的路线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

在调用onResponse被调用后,您应该在响应达到后设置别名。但是现在你在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) { 

     } 
    }); 
} 
+0

这意味着视图的初始化必须在initAccount()中进行? – Stansfield

+0

yes ofcourse。我确实已经更新了我的答案。使昵称文本视图全球。你可以尝试butterknife获取id的视图。我会很简单。不需要findViewByID。这是额外的建议。 :d –