logcat
06-09 07:40:37.396: E/AndroidRuntime(2328): FATAL EXCEPTION: main
06-09 07:40:37.396: E/AndroidRuntime(2328): Process: com.example.krishnaupahara, PID: 2328
06-09 07:40:37.396: E/AndroidRuntime(2328): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.krishnaupahara/com.example.krishnaupahara.BreakfastService}: java.lang.NullPointerException
06-09 07:40:37.396: E/AndroidRuntime(2328): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-09 07:40:37.396: E/AndroidRuntime(2328): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-09 07:40:37.396: E/AndroidRuntime(2328): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-09 07:40:37.396: E/AndroidRuntime(2328): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-09 07:40:37.396: E/AndroidRuntime(2328): at android.os.Handler.dispatchMessage(Handler.java:102)
06-09 07:40:37.396: E/AndroidRuntime(2328): at android.os.Looper.loop(Looper.java:136)
06-09 07:40:37.396: E/AndroidRuntime(2328): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-09 07:40:37.396: E/AndroidRuntime(2328): at java.lang.reflect.Method.invokeNative(Native Method)
06-09 07:40:37.396: E/AndroidRuntime(2328): at java.lang.reflect.Method.invoke(Method.java:515)
06-09 07:40:37.396: E/AndroidRuntime(2328): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-09 07:40:37.396: E/AndroidRuntime(2328): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-09 07:40:37.396: E/AndroidRuntime(2328): at dalvik.system.NativeStart.main(Native Method)
06-09 07:40:37.396: E/AndroidRuntime(2328): Caused by: java.lang.NullPointerException
06-09 07:40:37.396: E/AndroidRuntime(2328): at com.example.krishnaupahara.BreakfastService.showCountrySelection(BreakfastService.java:118)
06-09 07:40:37.396: E/AndroidRuntime(2328): at com.example.krishnaupahara.BreakfastService.onCreate(BreakfastService.java:62)
06-09 07:40:37.396: E/AndroidRuntime(2328): at android.app.Activity.performCreate(Activity.java:5231)
06-09 07:40:37.396: E/AndroidRuntime(2328): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-09 07:40:37.396: E/AndroidRuntime(2328): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-09 07:40:37.396: E/AndroidRuntime(2328): ... 11 more
06-09 07:40:37.506: W/ActivityManager(380): Force finishing activity com.example.krishnaupahara/.BreakfastService
java main file
package com.example.krishnaupahara;
import java.util.ArrayList;
import beanclass.Bfs;
import beanclass.ItemVO;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class BreakfastService extends Activity {
private ListView ls;
ArrayAdapter<String> arrayAdapter;
private ArrayList<String> itemList;
private ArrayList<Bfs> list;
private ArrayList<ItemVO> details;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.breakfast_service);
list = Utility.getmDetails(this);
ls = (ListView)findViewById(R.id.list);
details = new ArrayList<ItemVO>();
Log.d("TestTag", "Services : "+list.size());
sp = getSharedPreferences("aksp", Context.MODE_PRIVATE);
String services = sp.getString("services", null);
String[] itemArray = null;
itemList = new ArrayList<String>();
if(services == null || services.equalsIgnoreCase("")){
showCountrySelection();
}else{
itemArray = services.split("\\|");
for(String c : itemArray){
itemList.add(c);
}
setupScreen();
}
}
private void setupScreen(){
arrayAdapter = new ArrayAdapter<String>(this, R.layout.breakfast_service, R.id.textView);
details.clear();
for(Bfs service: list){
Log.d("TestTag", "Inside item for loop " + itemList.size());
if(itemList.contains(service.getCategoryName())){
Log.d("TestTag", "Inside item for loop's if condn");
service.setEnabled(true);
for(ItemVO d : service.getGetItemDetails()){
Log.d("TestTag", "Details per item "+d.getCategoryName());
arrayAdapter.add(d.getCategoryName());
details.add(d);
}
}
}
ls.setAdapter(new MyAdapter(BreakfastService.this, R.id.textView, details));
ls.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
ApplicationCache.getInstance().setValue("value", details.get(position));
}
});
BreakfastService.this.getWindow().getDecorView().invalidate();
}
private void showCountrySelection(){
final Dialog dialog = new Dialog(BreakfastService.this);
dialog.setContentView(R.layout.breakfast_service);
ListView lv = (ListView) dialog.findViewById(R.id.list);
dialog.setCancelable(true);
dialog.setTitle("Select Services");
ItemListAdapter dataAdapter = new ItemListAdapter(this,R.layout.list, list);
lv.setAdapter(dataAdapter);
Button myButton = (Button) this.findViewById(R.id.button12);
myButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
String services = "";
int count = 0;
for(Bfs c : list)
{
if(c.isEnabled())
{
count++;
services = services +"|" + c.getCategoryName();
sp.edit().putString("services", services).commit();
}
}
if(count == 0){
Toast.makeText(BreakfastService.this, "Please select at least one item", Toast.LENGTH_LONG).show();
return;
}
itemList.clear();
String[] servicesArray = services.split("\\|");
for(String c : servicesArray){
itemList.add(c);
}
dialog.dismiss();
setupScreen();
}
});
dialog.getWindow().getDecorView().invalidate();
dialog.show();
}
private class MyAdapter extends ArrayAdapter<ItemVO>{
private Context context;
private ArrayList<ItemVO> list;
private int resId;
public MyAdapter(Context context, int textViewResourceId, ArrayList<ItemVO> list) {
super(context, textViewResourceId, list);
this.context = context;
this.list=list;
resId = textViewResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
{
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.list, null);
}
TextView textview = (TextView) convertView.findViewById(R.id.textViewi);
ImageView imageview = (ImageView) convertView.findViewById(R.id.imageViewi);
Log.d("TestTag", "Item Name : "+textview );
imageview.setImageResource(list .get(position).getItemphoto());
textview.setText(list .get(position).getItemdescription());
return convertView;
}
}
}
xml parsing code..
package com.example.krishnaupahara;
import java.io.InputStream;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import beanclass.*;
import android.content.Context;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
public class Utility
{
private static ArrayList<Bfs> mDetails;
public static void init(Context context)
{
getmDetails(context);
}
public static ArrayList<Bfs> getmDetails(Context context)
{
if(mDetails == null)
{
mDetails = new ArrayList<Bfs>();
try {
Document doc = parseXML(readFromFile("bs.xml",context));
NodeList node = doc.getElementsByTagName("Category");
for (int i=0; i<node.getLength(); i++)
{
Bfs mtype = new Bfs();
ArrayList<ItemVO> ServiceName = new ArrayList<ItemVO>();
Log.d("Test", "length"+node.getLength());
Element e = (Element) node.item(i);
Element categoryNameE = (Element) e.getElementsByTagName("CategoryName").item(0);
Element categoryCodeE = (Element) e.getElementsByTagName("CategoryCode").item(0);
String categoryNameS = categoryNameE.getLastChild().getNodeValue();
String categoryCodeS = categoryCodeE.getLastChild().getNodeValue();
mtype.setCategoryName(categoryNameS);
mtype.setCategoryCode(categoryCodeS);
Log.d("TestTag", "MerchantTypeName from XML is :" +categoryNameS +""+categoryCodeS );
mtype.setCategoryName(categoryNameS);
NodeList node2 = e.getElementsByTagName("Item");
for (int k=0; k<node2.getLength();k++)
{
ItemVO ItemS = new ItemVO();
Log.d("Test", "length22"+node2.getLength());
Element mItemEl = (Element) node2.item(k);
Element itemCodeEl = (Element) mItemEl.getElementsByTagName("Itemcode").item(0);
Element itemDescriptionEl = (Element) mItemEl.getElementsByTagName("Itemdesription").item(0);
Element itemPhotoEl = (Element) mItemEl.getElementsByTagName("Itemphoto").item(0);
ItemS.setItemdescription(itemDescriptionEl.getLastChild().getNodeValue());
ItemS.setItemphoto((Integer.valueOf(itemPhotoEl.getLastChild().getNodeValue())));
ItemS.setCategoryName(categoryNameS);
ItemS.setItemcode(itemCodeEl.getLastChild().getNodeValue());
ServiceName.add(ItemS);
}
mtype.setGetItemDetails(ServiceName);
mDetails.add(mtype);
Log.d("TestTag", "Values in arraylist in XML"+mDetails.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
return mDetails;
}
public static Document parseXML(InputSource source) throws Exception
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db;
Document doc = null;
try {
db = dbf.newDocumentBuilder();
doc = db.parse(source);
} catch (Exception e) {
throw e;
}
return doc;
}
public static InputSource readFromFile(String fileName, Context context)
{
InputSource inputSource = null;
InputStream fIn = null;
try {
fIn = context.getResources().getAssets().open(fileName);
inputSource = new InputSource(fIn);
} catch (Exception e) {
e.printStackTrace();
}
return inputSource;
}
}
breakfast xml file
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<listview> android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<Button
android:id="@+id/button12"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:text="@string/addtocart" />
list xml file
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"> android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<linearlayout> android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dip"
android:removed="#336699" >
<ImageView
android:id="@+id/imageViewi"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/ic_launcher" />
<textview>
android:id="@+id/textViewi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginTop="8dip"
android:text="Middle Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<checkbox>
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Checkbox" />
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"> android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<linearlayout> android:id="@+id/ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dip"
android:removed="#336699" >
<ImageView
android:id="@+id/imageViewi"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="@drawable/ic_launcher" />
<textview>
android:id="@+id/textViewi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:layout_marginTop="8dip"
android:text="Middle Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<checkbox>
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Checkbox" />