I am new android.I want to develop an android app in which there is a notification badge on action bar.But the application is not starting.In my activity there is a list which is fetching from server and a dialog on click of list item. There is a null point exception at starting of application.Please guide to solve this error.I am referring this link for making notification badge:- Notification Badge On Action Item Android
error:-
Process: com.example.chaitanya.yoga, PID: 4497
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.example.chaitanya.yoga.record.onCreateOptionsMenu(record.java:201)
at android.app.Activity.onCreatePanelMenu(Activity.java:2869)
at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:548)
at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:917)
at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:258)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5237)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
record.class
public class record extends Activity {
.....
ArrayList<String> asd;
private ListView listView;
String info[];
String info1[];
String info2[];
String info3[];
String info4[];
String info5[];
static int abc;
int counter = 1;
TextView textCartItemCount;
int mCartItemCount = 10;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.record);
asd = new ArrayList<String>();
new gro().execute();
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
idName = prefs.getInt("idName", 0);
Intent intent = new Intent(record.this,
BackgroundService.class);
intent.putExtra("counter", counter++);
startService(intent);
}
class gro extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
...
}
protected String doInBackground(String... args) {
.....
}
} catch (JSONException e) {
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
class ViewDialog {
public void showDialog(Activity activity, int pos) {
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.dialog);
TextView text = (TextView) dialog.findViewById(R.id.nm1);
TextView text1 = (TextView) dialog.findViewById(R.id.rolln);
TextView text2 = (TextView) dialog.findViewById(R.id.bran);
TextView text3 = (TextView) dialog.findViewById(R.id.mobi);
TextView text4 = (TextView) dialog.findViewById(R.id.emai);
TextView text5 = (TextView) dialog.findViewById(R.id.dobb);
text.setText(info[pos]);
text1.setText("RollNo: " + info1[asd.size() - pos - 1]);
text2.setText("Branch: " + info2[asd.size() - pos - 1]);
text3.setText("MobileNo: " + info3[asd.size() - pos - 1]);
text4.setText("EmailId: " + info4[asd.size() - pos - 1]);
text5.setText("DateOfBirth: " + info5[asd.size() - pos - 1]);
Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
dialogButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
}
......
Collections.reverse(Arrays.asList(info));
customList = new CustomList(record.this, info);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(customList);
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
ViewDialog alert = new ViewDialog();
alert.showDialog(record.this, position);
return false;
}
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final MenuItem menuItem = menu.findItem(R.id.action_cart);
View actionView = MenuItemCompat.getActionView(menuItem);
textCartItemCount = (TextView) actionView.findViewById(R.id.cart_badge);
setupBadge();
actionView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onOptionsItemSelected(menuItem);
}
});
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_cart: {
Toast.makeText(this, "item clickeddddd",
Toast.LENGTH_LONG).show();
textCartItemCount.setVisibility(View.GONE);
return true;
}
}
return super.onOptionsItemSelected(item);
}
private void setupBadge() {
if (textCartItemCount != null) {
if (mCartItemCount == 0) {
if (textCartItemCount.getVisibility() != View.GONE) {
textCartItemCount.setVisibility(View.GONE);
}
} else {
textCartItemCount.setText(String.valueOf(Math.min(mCartItemCount, 99)));
if (textCartItemCount.getVisibility() != View.VISIBLE) {
textCartItemCount.setVisibility(View.VISIBLE);
}
}
}
}
}
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chaitanya.yoga" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
>
<activity android:name=".record" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".BackgroundService"
android:enabled="true">
</service>
</application>
menu_main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="@+id/action_cart"
android:icon="@mipmap/ic_launcher"
android:title="Cart"
app:actionLayout="@layout/custom_action_item_layout"
app:showAsAction="always"/>
</menu>
badge_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@android:color/holo_red_dark"/>
<stroke android:color="@android:color/white" android:width="1dp"/>
</shape>
record.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.chaitanya.yoga.record">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView" />
custom_action_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginEnd="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="3dp"
android:background="@drawable/badge_background"
android:gravity="center"
android:padding="3dp"
android:textColor="@android:color/white"
android:text="0"
android:textSize="10sp"/>