尝试调用虚拟方法'android.view.View android.view.View.findViewById(int)'

时间:2018-06-30 08:38:19

标签: android nullpointerexception cart shopping-cart badge

在com.thoughtworks.mythoughtworks.MainActivity.onCreateOptionsMenu(MainActivity.java:66)上显示错误->,并且显示空指针异常  当我在onCreateOptionMenu中加载通知徽章时...  请帮助

  

MainActivity.java

public class MainActivity extends AppCompatActivity {


ProgressDialog progress;
ListView ls;
int count = 0;
SearchView searchView;
NotificationBadge badge;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ls = (ListView)findViewById(R.id.listView);
    progress = new ProgressDialog(this);
    BackgroundTask process = new BackgroundTask();
    process.execute();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_main,menu);
    View mview = menu.findItem(R.id.cart).getActionView();
    badge = (NotificationBadge) mview.findViewById(R.id.badge);
    //updateCartCount();
    return true;
}
private void updateCartCount(){
    if(badge == null) return;
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            badge.setVisibility(View.VISIBLE);
            badge.setText("2");
        }
    });
}
@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
        case R.id.cart:
            Toast.makeText(this, "cart", Toast.LENGTH_SHORT).show();
            break;

1 个答案:

答案 0 :(得分:0)

您忘记为自己的TextView findViewById呼叫mTextViewResult