我有一个服务,用于解析xml数据(使用sax)之后我想要启动一个活动来显示使用intent解析的数据。但是每次我启动活动时我的服务都会崩溃,所以我删除了意图并改为举杯祝愿==>服务很好。但我真的需要从服务启动活动以使用xml rpc进行查询。我的小指告诉我它与上下文有关,但我对上下文一无所知。
这是我的服务:(问题在于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 tfe.rma.ciss.be.ParseFunction@4051ecc8 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
答案 0 :(得分: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
的字段?
答案 1 :(得分:0)
而不是
Intent intent1= new Intent (this, Test.class);
尝试:
Intent intent1= new Intent (getApplicationContext(), Test.class);
如果这不起作用,请尝试不添加标记。
但是,请记住,您不应该从服务启动活动或与UI交互。
答案 2 :(得分:0)
//此处您的内容为空
Intent intent1= new Intent (this, Test.class);
在此处传递您的申请上下文
Intent intent1= new Intent (mContent, Test.class);