下面有logcat错误请提示我哪里错了以及所有代码 [英] below have logcat errors pls suggest me where its wrong and below all codes

查看:58
本文介绍了下面有logcat错误请提示我哪里错了以及所有代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

logcat





06-09 07:40:37.396:E / AndroidRuntime(2328):致命异常:主要

06-09 07:40:37.396:E / AndroidRuntime(2328):处理:com.example.krishnaupahara,PID:2328

06-09 07:40:37.396: E / AndroidRuntime(2328):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.krishnaupahara / com.example.krishnaupahara.BreakfastService}:java.lang.NullPointerException

06-09 07:40:37.396:E / AndroidRuntime(2328):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)

06-09 07:40:37.396:E / AndroidRuntime(2328) ):在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):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)

06-09 07:40:37.396:E / AndroidRun时间(2328):在android.os.Handler.dispatchMessage(Handler.java:102)

06-09 07:40:37.396:E / AndroidRuntime(2328):在android.os.Looper .loop(Looper.java:136)

06-09 07:40:37.396:E / AndroidRuntime(2328):在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):引起:java.lang.NullPointerException

06-09 07:40:37.396: E / A ndroidRuntime(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):在android.app.Activity.performCreate(Activity.java: 5231)

06-09 07:40:37.396:E / AndroidRuntime(2328):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

06 -09 07:40:37.396:E / AndroidRuntime(2328):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

06-09 07:40:37.396:E / AndroidRuntime (2328):... 11更多

06-09 07:40:37.506:W / ActivityManager(380):强制完成活动com.example.krishnaupahara / .BreakfastService





java主文件





 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>列表;
private ArrayList< ItemVO>细节;
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());

/ * Button myButton =(Button)this.findViewById(R.id.button12);
myButton.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v){

showCountrySelection();

}
});
* /
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服务:列表){

Log.d(TestTag,I​​nside item for loop+ itemList.size());
if(itemList.contains(service.getCategoryName())){
Log.d(TestTag,I​​nside item for loop's if condn);
service.setEnabled(true);
for(ItemVO d:service.getGetItemDetails()){
Log.d(TestTag,每个项目的详细信息+ d.getCategoryName());
arrayAdapter.add(d.getCategoryName());
details.add(d);
}
}
}

ls.setAdapter(新的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));

/ * Intent detailsIntent = new Intent( ApnaKhata.this,BankFunctionalities.class);
startActivity(detailsIntent); * /
}
});
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(选择服务);

ItemListAdapter dataAdapter = new ItemListAdapter(this,R.layout.list,list);

lv.setAdapter(dataAdapter);

按钮myButton =(按钮)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,请选择至少一个项目 ,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();

}

私有类MyAdapter扩展ArrayAdapter< ItemVO> {

私有上下文上下文;
private ArrayList< ItemVO>列表;
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,项目名称:+ textview);
imageview.setImageResource(list .get(position).getItemphoto());
textview.setText(list .get(position).getItemdescription());


/ * if(position == 0){
textview.setVisibility(View.VISIBLE);
textview.setText(list.get(position).getItemdescription());
//header.setImageResource(list .get(position).getItemphoto());
}
else if(position> 0&& position< list.size() - 1&&!list.get(position).getItemdescription()。
equalsIgnoreCase(list.get(position-1).getItemdescription())){
textview.setVisibility(View.VISIBLE);
textview.setText(list.get(position).getItemdescription());
}
* /
return convertView;
}

}

}













xml解析代码..



< br $> b $ b

 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());


元素e =(元素)node.item(i);

元素categoryNameE =(元素)e.getElementsByTagName(CategoryName)。item(0);
元素categoryCodeE =(元素)e.getElementsByTagName(CategoryCode)。item(0);

String categoryNameS = categoryNameE.getLastChild()。getNodeValue();
String categoryCodeS = categoryCodeE.getLastChild()。getNodeValue();

mtype.setCategoryName(categoryNameS);
mtype.setCategoryCode(categoryCodeS);


Log.d(TestTag,来自XML的MerchantTypeName是:+ 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());
元素mItemEl =(元素)node2.item(k);

元素itemCodeEl =(元素)mItemEl.getElementsByTagName(Itemcode)。item(0);
元素itemDescriptionEl =(元素)mItemEl.getElementsByTagName(Itemdesription)。item(0);
元素itemPhotoEl =(元素)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,XML中的arraylist中的值+ mDetails.toString());
}
} catch(例外e){
// TODO自动生成的catch块
e.printStackTrace();
}
}

返回mDetails;
} // getmDetails结束

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(异常e){
// TODO自动生成的catch块
throw e;
}

返回doc;
} //结束parseXML

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(例外e){
// TODO自动生成的catch块
e.printStackTrace();
}

返回inputSource;
}

}











早餐xml文件











< 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/>













列表xml文件











< 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 =中间文字

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 =中间文字

android:textAppearance =?android:attr / textAppearanceMedium/> ;



< checkbox>

android:id =@ + id / checkBox1

android:layout_width =wrap_content

android:layout_height = wrap_content

android:text =@ string / Checkbox/>






解决方案

800(ActivityThread.java:135)

06-09 07:40:37.396:E / AndroidRuntime(2328) :在android.app.ActivityThread


H.handleMessage(ActivityThread.java:1196)

06-09 07:40:37.396:E / AndroidRuntime(2328) ):在android.os.Handler.dispatchMessage(Handler.java:102)

06-09 07:40:37.396:E / AndroidRuntime(2328):在android.os.Looper.loop( Looper.java:136)

06-09 07:40:37.396:E / AndroidRuntime(2328):在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) ):在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):引起: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):在android.app.Activity.performCreate(Activity.java:5231)

06-09 07 :40:37.396:E / AndroidRuntime(2328):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

06-09 07:40:37.396:E / AndroidRuntime(2328) :在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

06-09 07:40:37.396:E / AndroidRuntime(2328):... 11更多

06-09 07:40:37.506:W / ActivityManager(380):强制完成活动com.example.krishnaupahara / .BreakfastService





java主文件





 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>列表;
private ArrayList< ItemVO>细节;
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());

/ * Button myButton =(Button)this.findViewById(R.id.button12);
myButton.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v){

showCountrySelection();

}
});
* /
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服务:列表){

Log.d(TestTag,I​​nside item for loop+ itemList.size());
if(itemList.contains(service.getCategoryName())){
Log.d(TestTag,I​​nside item for loop's if condn);
service.setEnabled(true);
for(ItemVO d:service.getGetItemDetails()){
Log.d(TestTag,每个项目的详细信息+ d.getCategoryName());
arrayAdapter.add(d.getCategoryName());
details.add(d);
}
}
}

ls.setAdapter(新的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));

/ * Intent detailsIntent = new Intent( ApnaKhata.this,BankFunctionalities.class);
startActivity(detailsIntent); * /
}
});
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(选择服务);

ItemListAdapter dataAdapter = new ItemListAdapter(this,R.layout.list,list);

lv.setAdapter(dataAdapter);

按钮myButton =(按钮)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,请选择至少一个项目 ,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();

}

私有类MyAdapter扩展ArrayAdapter< ItemVO> {

私有上下文上下文;
private ArrayList< ItemVO>列表;
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,项目名称:+ textview);
imageview.setImageResource(list .get(position).getItemphoto());
textview.setText(list .get(position).getItemdescription());


/ * if(position == 0){
textview.setVisibility(View.VISIBLE);
textview.setText(list.get(position).getItemdescription());
//header.setImageResource(list .get(position).getItemphoto());
}
else if(position> 0&& position< list.size() - 1&&!list.get(position).getItemdescription()。
equalsIgnoreCase(list.get(position-1).getItemdescription())){
textview.setVisibility(View.VISIBLE);
textview.setText(list.get(position).getItemdescription());
}
* /
return convertView;
}

}

}













xml解析代码..



< br $> b $ b

 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());


元素e =(元素)node.item(i);

元素categoryNameE =(元素)e.getElementsByTagName(CategoryName)。item(0);
元素categoryCodeE =(元素)e.getElementsByTagName(CategoryCode)。item(0);

String categoryNameS = categoryNameE.getLastChild()。getNodeValue();
String categoryCodeS = categoryCodeE.getLastChild()。getNodeValue();

mtype.setCategoryName(categoryNameS);
mtype.setCategoryCode(categoryCodeS);


Log.d(TestTag,来自XML的MerchantTypeName是:+ 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());
元素mItemEl =(元素)node2.item(k);

元素itemCodeEl =(元素)mItemEl.getElementsByTagName(Itemcode)。item(0);
元素itemDescriptionEl =(元素)mItemEl.getElementsByTagName(Itemdesription)。item(0);
元素itemPhotoEl =(元素)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,XML中的arraylist中的值+ mDetails.toString());
}
} catch(例外e){
// TODO自动生成的catch块
e.printStackTrace();
}
}

返回mDetails;
} // getmDetails结束

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(异常e){
// TODO自动生成的catch块
throw e;
}

返回doc;
} //结束parseXML

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(例外e){
// TODO自动生成的catch块
e.printStackTrace();
}

返回inputSource;
}

}











早餐xml文件











< 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/>













列表xml文件











< 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 =中间文字

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 =中间文字

android:textAppearance =?android:attr / textAppearanceMedium/> ;



< checkbox>

android:id =@ + id / checkBox1

android:layout_width =wrap_content

android:layout_height = wrap_content

android:text =@ string / Checkbox/>







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());
		
		/*Button myButton = (Button) this.findViewById(R.id.button12);
		myButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				showCountrySelection();

			}
		});
*/
		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));

			/*	Intent detailsIntent = new Intent(ApnaKhata.this,BankFunctionalities.class);
				startActivity(detailsIntent);*/
			}
		});
		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());
	        

		/*if(position == 0){
				textview.setVisibility(View.VISIBLE);
				textview.setText(list.get(position).getItemdescription());
				//header.setImageResource(list .get(position).getItemphoto());
			}
			else if(position > 0 && position < list.size()-1 && !list.get(position).getItemdescription().
					equalsIgnoreCase(list.get(position-1).getItemdescription())){
				textview.setVisibility(View.VISIBLE);
				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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		
		return mDetails;
	}// End of getmDetails

	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) {
			// TODO Auto-generated catch block
			throw e;
		}
		
		return doc;
	}// End of parseXML

	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) {
			// TODO Auto-generated catch block
			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" />



解决方案

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());
		
		/*Button myButton = (Button) this.findViewById(R.id.button12);
		myButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				showCountrySelection();

			}
		});
*/
		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));

			/*	Intent detailsIntent = new Intent(ApnaKhata.this,BankFunctionalities.class);
				startActivity(detailsIntent);*/
			}
		});
		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());
	        

		/*if(position == 0){
				textview.setVisibility(View.VISIBLE);
				textview.setText(list.get(position).getItemdescription());
				//header.setImageResource(list .get(position).getItemphoto());
			}
			else if(position > 0 && position < list.size()-1 && !list.get(position).getItemdescription().
					equalsIgnoreCase(list.get(position-1).getItemdescription())){
				textview.setVisibility(View.VISIBLE);
				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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
		
		return mDetails;
	}// End of getmDetails

	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) {
			// TODO Auto-generated catch block
			throw e;
		}
		
		return doc;
	}// End of parseXML

	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) {
			// TODO Auto-generated catch block
			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" />




这篇关于下面有logcat错误请提示我哪里错了以及所有代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆