Android 应用程序不断重新启动
我试图将数据从PostData类的内部类Asynctask中的DoinBackground方法传递到MainActivity类的内部类LocationListener中的OnLocationChanged方法。目前我正在重启应用程序。我想确认服务器的前5个响应是空的。我怎样才能解决这个问题 错误:Android 应用程序不断重新启动,android,Android,我试图将数据从PostData类的内部类Asynctask中的DoinBackground方法传递到MainActivity类的内部类LocationListener中的OnLocationChanged方法。目前我正在重启应用程序。我想确认服务器的前5个响应是空的。我怎样才能解决这个问题 错误: 05-17 15:07:21.778: I/Process(3224): Sending signal. PID: 3224 SIG: 9 05-17 15:07:26.683: D/Activity
05-17 15:07:21.778: I/Process(3224): Sending signal. PID: 3224 SIG: 9
05-17 15:07:26.683: D/Activity(8195): performCreate Call secproduct feature valuefalse
05-17 15:07:26.683: D/Activity(8195): performCreate Call debug elastic valuetrue
05-17 15:07:26.853: V/RenderScript(8195): 0xaf557200 Launching thread(s), CPUs 4
05-17 15:07:26.903: I/Timeline(8195): Timeline: Activity_idle id: android.os.BinderProxy@2a75908a time:366176691
05-17 15:07:26.993: D/AndroidRuntime(8195): Shutting down VM
05-17 15:07:26.993: E/AndroidRuntime(8195): FATAL EXCEPTION: main
05-17 15:07:26.993: E/AndroidRuntime(8195): Process: com.bustracker, PID: 8195
05-17 15:07:26.993: E/AndroidRuntime(8195): java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList android.os.Bundle.getIntegerArrayList(java.lang.String)' on a null object reference
05-17 15:07:26.993: E/AndroidRuntime(8195): at com.bustracker.MainActivity$myLocationListener.onLocationChanged(MainActivity.java:249)
05-17 15:07:26.993: E/AndroidRuntime(8195): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:281)
05-17 15:07:26.993: E/AndroidRuntime(8195): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:210)
05-17 15:07:26.993: E/AndroidRuntime(8195): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:226)
05-17 15:07:26.993: E/AndroidRuntime(8195): at android.os.Handler.dispatchMessage(Handler.java:102)
05-17 15:07:26.993: E/AndroidRuntime(8195): at android.os.Looper.loop(Looper.java:145)
05-17 15:07:26.993: E/AndroidRuntime(8195): at android.app.ActivityThread.main(ActivityThread.java:5944)
05-17 15:07:26.993: E/AndroidRuntime(8195): at java.lang.reflect.Method.invoke(Native Method)
05-17 15:07:26.993: E/AndroidRuntime(8195): at java.lang.reflect.Method.invoke(Method.java:372)
05-17 15:07:26.993: E/AndroidRuntime(8195): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
05-17 15:07:26.993: E/AndroidRuntime(8195): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
主要活动:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener ll = new myLocationListener(this);
// location updates: at least 0 meter and 60 seconds change.
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 0, ll);
}
//Inner class in MainActivity
class myLocationListener implements LocationListener {
final Context bContext;
public myLocationListener(Context context){
bContext = context;
}
@Override
public void onLocationChanged(Location location) {
PostData sender = new PostData();
sender.post_data(jSONString, bContext);
//receive the Intent.
Bundle b = getIntent().getExtras();
b.getIntegerArrayList("stop_route");
}
}
}
PostData类:
public class PostData {
String jSONString;
public PostData() {
super();
}
public void post_data(String jSONString, Context context) {
this.jSONString = jSONString;
new MyAsyncTask(context).execute(jSONString);
}
//Inner class in ht PostData class.
class MyAsyncTask extends AsyncTask<String, Integer, Void> {
final Context mContext;
public MyAsyncTask(Context context){
mContext = context;
}
@Override
protected Void doInBackground(String... params) {
ArrayList<Integer> routes = data.getRoutes();
Intent intent = new Intent(mContext , MainActivity.class);
intent.putExtra("stop_route" ,routes);
mContext.startActivity(intent);
return null;
}
}
}
公共类PostData{
字符串jSONString;
公共PostData(){
超级();
}
public void post_数据(字符串jSONString,上下文){
this.jSONString=jSONString;
新建MyAsyncTask(context).execute(jSONString);
}
//ht PostData类中的内部类。
类MyAsyncTask扩展了AsyncTask{
最终语境;
公共MyAsyncTask(上下文){
mContext=上下文;
}
@凌驾
受保护的Void doInBackground(字符串…参数){
ArrayList routes=data.getRoutes();
意向意向=新意向(mContext,MainActivity.class);
意向。额外(“停止路线”,路线);
mContext.startActivity(意图);
返回null;
}
}
}
@凌驾
受保护的Void doInBackground(字符串…参数){
}
doInBackground进程在UI主线程中不工作,startActivity需要从主线程调用,所以请尝试使用
类MyAsyncTask扩展了AsyncTask{
最终语境;
ArrayList路线;
公共MyAsyncTask(上下文){
mContext=上下文;
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
routes=newarraylist();
}
@凌驾
受保护的Void doInBackground(字符串…参数){
routes=data.getRoutes();
返回null;
}
@凌驾
受保护的void onPostExecute(长结果){
意向意向=新意向(mContext,MainActivity.class);
意向。额外(“停止路线”,路线);
mContext.startActivity(意图);
}
}
getItent()。getExtras()正在返回null@Blackbelt:是的,它返回null,但我有时会得到一个结果,因此我将它放在if语句
Bundle extras=getIntent().getExtras()中;如果(extras!=null){}
但我是尝试在null对象引用上调用虚拟方法“java.util.Iterator java.util.ArrayList.Iterator()”
@Rami:但是我如何将doInbackground方法中的路由传递给它呢?由于我遇到此错误,无法将路由解析为变量
将其声明为字符串jSONString;//阶级var@Rami:问题是routes很长一段时间没有值,然后我在一个空对象引用上得到了这个错误Bundle.getIntegerArrayList(java.lang.String)
在接收到意图的地方,处理这个错误的最佳方法是什么?因为我在接收到if语句后放置了一个if语句来检查它是否为空,但没有成功。Ramy forprotected void onPostExecute(Long result)
我得到的是PostData类型的onPostExecute(Long)方法。MyAsyncTask必须重写或实现一个超类型方法我认为从AsyncTask参数
class MyAsyncTask extends AsyncTask<String, Integer, Void> {
final Context mContext;
ArrayList<Integer> routes;
public MyAsyncTask(Context context){
mContext = context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
routes = new ArrayList<Integer>();
}
@Override
protected Void doInBackground(String... params) {
routes = data.getRoutes();
return null;
}
@Override
protected void onPostExecute(Long result) {
Intent intent = new Intent(mContext , MainActivity.class);
intent.putExtra("stop_route" ,routes);
mContext.startActivity(intent);
}
}