Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' [duplicate]


Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' [duplicate]



This question already has an answer here:



It is showing error -> at com.thoughtworks.mythoughtworks.MainActivity.onCreateOptionsMenu(MainActivity.java:66) .It is showing null pointer exception
when I load notification badge in onCreateOptionMenu...
Please help



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;



This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




1 Answer
1



you forget call findViewById for your TextView mTextViewResult


findViewById


mTextViewResult





sorry, I have edit my code.There is nothing of mTextViewResult
– Shivam Varshney
Jun 30 at 8:47





you must send your log cat error and xml layout
– Radesh
Jun 30 at 8:57

Popular posts from this blog

Why are these constructs (using ++) undefined behavior in C?

I'm Still Waiting (Diana Ross song)

Delphi Android file open failure with API 26