2012-01-12 110 views
0

我有一个服务,我用它来解析XML数据(使用SAX)之后我想要启动一个活动来显示使用意图。但每次我启动活动时,我的服务崩溃,所以我删除了意图,并把烤面包而不是==>服务工作正常。但我真的需要从服务启动活动,以使用xml rpc进行查询。我的小手指告诉我,这与情境有关,但我对情境一无所知。无法从Android中的服务类启动活动(java.lang.NullPointerException)

这里是我的服务:在这里(的问题在于intent1)

package tfe.rma.ciss.be; 

import java.io.IOException; 
import java.io.StringReader; 
import java.net.MalformedURLException; 

import javax.xml.parsers.ParserConfigurationException; 
import javax.xml.parsers.SAXParser; 
import javax.xml.parsers.SAXParserFactory; 

import org.xml.sax.Attributes; 
import org.xml.sax.InputSource; 
import org.xml.sax.SAXException; 
import org.xml.sax.XMLReader; 
import org.xml.sax.helpers.DefaultHandler; 

import android.app.Service; 
import android.content.Intent; 
import android.os.IBinder; 
import android.util.Log; 
import android.widget.Toast; 

public class ParseFunction extends Service{ 
    Server monServer; 
    public String streamTitle = "",path=""; 
    public void onCreate() { 

     Log.d("ParseFunction", ">>>onCreate()"); 

    } 

    @Override 
    public void onStart (Intent intent, int startId) { 
     Log.d("ParseFunction", ">>>Started()"); 
     if (!mydata.equals("")){ 
     try { 

      // URL rssUrl = new URL("http://thibault-koprowski.fr/feed"); 
       SAXParserFactory mySAXParserFactory = SAXParserFactory.newInstance(); 
       SAXParser mySAXParser = mySAXParserFactory.newSAXParser(); 
       XMLReader myXMLReader = mySAXParser.getXMLReader(); 
       RSSHandler myRSSHandler = new RSSHandler(); 
       myXMLReader.setContentHandler(myRSSHandler); 
       InputSource input = new InputSource(new StringReader(monServer.myData)); 
       myXMLReader.parse(input); 
       Log.d("ParseFunction", ">>>my data is parsed()" + streamTitle); 

      } catch (MalformedURLException e) { 
       Log.d("ParseFunction", ">>>exception()"); 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       Log.d("ParseFunction", ">>>exception()"); 
      } catch (ParserConfigurationException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       Log.d("ParseFunction", ">>>exception()"); 
      } catch (SAXException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
       Log.d("ParseFunction", ">>>exception()"); 
      } catch (IOException e) { 
       Log.d("ParseFunction", ">>>exception()"); 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 

      } 
     } 
     else {} 
     if (!streamTitle.equals("")) { 
      Log.d("ParseFunction", ">>>splitting streamTitle"); 
      try { 
      String delims = "[\n]+" ; 
       String[] tokens = streamTitle.split(delims) ; 
       path= tokens[1]; 
       Log.d("ParseFunction", ">>>splitting done"); 

       Toast.makeText(this,path, Toast.LENGTH_SHORT).show(); 
        } 
      catch (Exception z) { 
       Log.d("ParseFunction", ">>>splitting error" + " " + z); 

      } 
     } else 
     { path="n'a pas pu parser" + "/n" + "no path found"; } 

     try { 

      Log.d("ParseFunction", ">>>lauching activity test but ...."); 
     Intent intent1= new Intent (this, Test.class); 
     intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
     intent1.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 



     startActivity(intent1); } catch (Exception z) { 

      Log.d("ParseFunction", ">>>Activity said hell no, to screw myself " + z); 

     } 


    } 
    private class RSSHandler extends DefaultHandler 
     { 
     final int stateUnknown = 0; 
     final int stateTitle = 1; 
     int state = stateUnknown; 

     int numberOfTitle = 0; 
     String strTitle = ""; 
     String strElement = ""; 

    @Override 
    public void startDocument() throws SAXException { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void endDocument() throws SAXException { 
     // TODO Auto-generated method stub 

     streamTitle = strTitle; 
    } 

    @Override 
    public void startElement(String uri, String localName, String qName, 
     Attributes attributes) throws SAXException { 
     // TODO Auto-generated method stub 
     if (localName.equalsIgnoreCase("sensorType")) 
     { 
     state = stateTitle; 
     strElement = ""; 
     numberOfTitle++; 
     } 
     else if (localName.equalsIgnoreCase("imagePath")) 
     { 
     state = stateTitle; 
     strElement = ""; 
     numberOfTitle++; 
     } 
     else if (localName.equalsIgnoreCase("numThreats")) 
     { 
     state = stateTitle; 
     strElement = ""; 
     numberOfTitle++; 
     } 

     else if (localName.equalsIgnoreCase("latitude")) 
     { 
     state = stateTitle; 
     strElement = ""; 
     numberOfTitle++; 
     } 
     else if (localName.equalsIgnoreCase("longitude")) 
     { 
     state = stateTitle; 
     strElement = ""; 
     numberOfTitle++; 
     } 
     else 
     { 
     state = stateUnknown; 
     } 
    } 

    @Override 
    public void endElement(String uri, String localName, String qName) 
     throws SAXException { 
     // TODO Auto-generated method stub 
     if (localName.equalsIgnoreCase("sensorType")) 
     { 
     strTitle += strElement + "\n"; 
     } 
     else if (localName.equalsIgnoreCase("imagePath")) 
     { 
     strTitle += strElement + "\n"; 
     } 
     else if (localName.equalsIgnoreCase("numThreats")) 
     { 
     strTitle += strElement + "\n"; 
     } 

     else if (localName.equalsIgnoreCase("latitude")) 
     { 
     strTitle += strElement + "\n"; 
     } 
     else if (localName.equalsIgnoreCase("longitude")) 
     { 
     strTitle += strElement + "\n"; 
     } 

     else { 
     state = stateUnknown;} 
    } 

    @Override 
    public void characters(char[] ch, int start, int length) 
     throws SAXException { 
     // TODO Auto-generated method stub 
     String strCharacters = new String(ch, start, length); 
     if (state == stateTitle) 
     { 
     strElement += strCharacters; 
     } 
    } 

     } 
    @Override 
    public IBinder onBind(Intent arg0) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
    public static String mydata = "<AvailableImageMsg><imageData><imageId>1234</imageId><sensorType>2</sensorType><imagePath>http://localhost/some/path</imagePath><timeStamp>2011-10-12T12:36:20</timeStamp></imageData><numThreats>3</numThreats><threatList><ThreadPixelDataType><threatData><threatId>3456</threatId><latitude>50.1234</latitude><longitude>4.5678</longitude><altitude>10.1</altitude><threatType>pepe</threatType><timestamp>2011-10-12T12:36:20</timestamp><threatLevel>70</threatLevel></threatData><Rectangle><PixelCoordinatesType><x>100</x><y>200</y></PixelCoordinatesType><PixelCoordinatesType><x>100</x><y>200</y></PixelCoordinatesType><PixelCoordinatesType><x>100</x><y>200</y></PixelCoordinatesType><PixelCoordinatesType><x>100</x><y>200</y></PixelCoordinatesType></Rectangle></ThreadPixelDataType></threatList><threatList><ThreadPixelDataType><threatData><threatId>3457</threatId><latitude>50.1234</latitude><longitude>4.5678</longitude><altitude>10.1</altitude><threatType>pepe</threatType><timestamp>2011-10-12T12:36:20</timestamp><threatLevel>70</threatLevel></threatData><Rectangle><PixelCoordinatesType><x>100</x><y>200</y></PixelCoordinatesType><PixelCoordinatesType><x>100</x><y>200</y></PixelCoordinatesType><PixelCoordinatesType><x>100</x><y>200</y></PixelCoordinatesType><PixelCoordinatesType><x>100</x><y>200</y></PixelCoordinatesType></Rectangle></ThreadPixelDataType></threatList><threatList><ThreadPixelDataType><threatData><threatId>3458</threatId><latitude>50.1234</latitude><longitude>4.5678</longitude><altitude>10.1</altitude><threatType>pepe</threatType><timestamp>2011-10-12T12:36:20</timestamp><threatLevel>70</threatLevel></threatData><Rectangle><PixelCoordinatesType><x>100</x><y>200</y></PixelCoordinatesType><PixelCoordinatesType><x>100</x><y>200</y></PixelCoordinatesType><PixelCoordinatesType><x>100</x><y>200</y></PixelCoordinatesType><PixelCoordinatesType><x>100</x><y>200</y></PixelCoordinatesType></Rectangle></ThreadPixelDataType></threatList></AvailableImageMsg>"; 


} 

是我简单的活动名为test

package tfe.rma.ciss.be; 


import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.widget.TextView; 

public class Test extends Activity{ 

    ParseFunction parser; 

    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.test); 

      TextView result = (TextView)findViewById(R.id.result); 




      result.setText(parser.path);} 



} 

有人可以帮我请????? !

logcat的报告

01-12 13:42:18.069 I/ActivityManager( 160): Start proc tfe.rma.ciss.be for service tfe.rma.ciss.be/.ParseFunction: pid=1576 uid=10065 gids={3003, 1015} 
01-12 13:42:18.189 D/ParseFunction(1576): >>>onCreate() 
01-12 13:42:18.189 D/ParseFunction(1576): >>>Started() 
01-12 13:42:18.189 D/AndroidRuntime(1576): Shutting down VM 
01-12 13:42:18.189 W/dalvikvm(1576): threadid=1: thread exiting with uncaught exception (group=0x40018578) 
01-12 13:42:18.199 E/  ( 160): Dumpstate > /data/log/dumpstate_app_error 
01-12 13:42:18.199 E/AndroidRuntime(1576): FATAL EXCEPTION: main 
01-12 13:42:18.199 E/AndroidRuntime(1576): java.lang.RuntimeException: Unable to start service [email protected] with Intent { cmp=tfe.rma.ciss.be/.ParseFunction }: java.lang.NullPointerException 
01-12 13:42:18.199 E/AndroidRuntime(1576):  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2056) 
01-12 13:42:18.199 E/AndroidRuntime(1576):  at android.app.ActivityThread.access$2800(ActivityThread.java:117) 
01-12 13:42:18.199 E/AndroidRuntime(1576):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:998) 
01-12 13:42:18.199 E/AndroidRuntime(1576):  at android.os.Handler.dispatchMessage(Handler.java:99) 
01-12 13:42:18.199 E/AndroidRuntime(1576):  at android.os.Looper.loop(Looper.java:123) 
01-12 13:42:18.199 E/AndroidRuntime(1576):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
01-12 13:42:18.199 E/AndroidRuntime(1576):  at java.lang.reflect.Method.invokeNative(Native Method) 
01-12 13:42:18.199 E/AndroidRuntime(1576):  at java.lang.reflect.Method.invoke(Method.java:507) 
01-12 13:42:18.199 E/AndroidRuntime(1576):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
01-12 13:42:18.199 E/AndroidRuntime(1576):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
01-12 13:42:18.199 E/AndroidRuntime(1576):  at dalvik.system.NativeStart.main(Native Method) 
01-12 13:42:18.199 E/AndroidRuntime(1576): Caused by: java.lang.NullPointerException 
01-12 13:42:18.199 E/AndroidRuntime(1576):  at tfe.rma.ciss.be.ParseFunction.onStart(ParseFunction.java:45) 
01-12 13:42:18.199 E/AndroidRuntime(1576):  at android.app.Service.onStartCommand(Service.java:428) 
01-12 13:42:18.199 E/AndroidRuntime(1576):  at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2043) 
01-12 13:42:18.199 E/AndroidRuntime(1576):  ... 10 more 
01-12 13:42:20.709 D/BatteryService( 160): update start 
01-12 13:42:20.709 D/BatteryService( 160): update start 
01-12 13:42:20.709 D/BatteryService( 160): update start 
01-12 13:42:22.079 D/VoldCmdListener( 87): asec list 
01-12 13:42:22.089 D/VoldCmdListener( 87): CommandListener::AsecCmd::runCommand -> com.aldiko.android-1 
01-12 13:42:22.089 D/VoldCmdListener( 87): CommandListener::AsecCmd::runCommand -> com.citc.ud-1 
01-12 13:42:22.089 D/VoldCmdListener( 87): CommandListener::AsecCmd::runCommand -> com.creativemobile.DragRacing-1 
+1

帖子异常堆栈跟踪。 – 2012-01-12 12:12:54

+0

您是否将您的活动添加到应用程序的AndroidManifest.xml中? – Tormod 2012-01-12 13:21:44

+0

是的,我做了它,当我在服务外的应用程序laucnh,它的工作原理(当然与一个静态文本) – youssoua 2012-01-12 13:29:58

回答

0
Intent intent1= new Intent (this, Test.class); 
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 

在你指的是intent而非intent1的最后一行。你有没有初始化的地方叫intent

+0

是的意图是为服务@Override \t公共无效在onStart(意向意向的OnStart的paramtres,诠释startId){ \t \t Log.d(” ParseFunction“,”>>> Started()“); \t \t \t if(!mydata.equals(“”)){ – youssoua 2012-01-12 12:23:49

+0

我的错误,它应该是intent1但仍然存在问题:-( – youssoua 2012-01-12 12:29:30

0

而不是

Intent intent1= new Intent (this, Test.class); 

尝试:

Intent intent1= new Intent (getApplicationContext(), Test.class); 

如果还是不行,请尝试不添加标志。

但是,请记住,不应该从服务启动活动或与UI进行交互。

+0

我试过了,但仍然有相同的错误。我希望我可以做其他事情,因为事情是我的android设备上有服务器等待图像数据,所以一旦它有数据传递给服务解析它,然后应该启动显示图像的活动。 – youssoua 2012-01-12 12:48:31

+0

然后用AsyncTask代替你的服务,AsyncTask会像你想要的那样在后台处理所有的服务,但它有一个名为OnPostExecute的方法,它将在UI线程中运行,你可以随心所欲地执行任何操作。 – sfratini 2012-01-12 12:54:02

+0

okido,我会尝试去了解更多关于AsyncTask ... thx的内容:-) – youssoua 2012-01-12 12:57:11

0

//此处是您的内容为空

Intent intent1= new Intent (this, Test.class); 

通过你的应用程序上下文这里

Intent intent1= new Intent (mContent, Test.class); 
+0

我试过了,但它仍然无法工作:( – youssoua 2012-01-12 12:50:38

相关问题