无法得到错误是什么以及如何解决它

时间:2015-02-12 11:01:08

标签: android json eclipse

*我正在通过JSON解析一些数据但是当我点击按钮开始这个解析活动时它停止工作所有应用程序都在工作,除了这个按钮4 *

按钮的主要活动

package com.example.googlemapandroidv2;

import com.google.android.gcm.GCMRegistrar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;


public class Mainpage extends Activity{


Button b1,b2,b3,b4;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.frontpage);

    GCMRegistrar.checkDevice(this);
    GCMRegistrar.checkManifest(this);
    GCMRegistrar.register(Mainpage.this,
            GCMIntentService.SENDER_ID);

    b1=(Button)findViewById(R.id.button1);
    //  b2.setBackgroundResource(R.drawable.bbmb);

        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //String url = "http://expressdiner.ifeelhungry.co.uk/Menu.aspx";
            //  Intent w = new Intent(Intent.ACTION_VIEW);
            //  w.setData(Uri.parse(url));
            //  startActivity(w);   

                Intent w=new Intent(getApplicationContext(), Webii.class);
            startActivity(w);




        }
            });

        b2=(Button)findViewById(R.id.button2);
        //  b2.setBackgroundResource(R.drawable.bbmb);

            b2.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                //  String url = "http://musicfeeds.com.au/feeds/miley-cyrus/";
            //      Intent w = new Intent(Intent.ACTION_VIEW);
                //  w.setData(Uri.parse(url));
                //  startActivity(w);   

                    Intent w=new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(w);




            }
                });

            b3=(Button)findViewById(R.id.button3);
            //  b2.setBackgroundResource(R.drawable.bbmb);

                b3.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub


                        Intent w=new Intent(getApplicationContext(), Conected.class);
                        startActivity(w);




                }});
                b4=(Button)findViewById(R.id.button4);
                //  b2.setBackgroundResource(R.drawable.bbmb);

                    b4.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            Intent intent = new        Intent(getApplicationContext(),Promotions.class);
                            startActivity(intent);  

                                                    }});


    }
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onBackPressed() {
    moveTaskToBack(true);
}

// Before 2.0
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        moveTaskToBack(true);
        return true;
    }
    return super.onKeyDown(keyCode, event);
}
@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    GCMRegistrar.onDestroy(Mainpage.this);
}



}

以下是第二项活动

package com.example.googlemapandroidv2;

import java.util.ArrayList;
import com.example.googlemapandroidv2.R;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Promotions extends ListActivity {

private Context context;
private static String url = "http://expressdiner.ifeelhungry.co.uk/pushadmin/webservice/getPermotions.php";

private static final String ID = "idj";
private static final String TITLE = "titlej";
private static final String URL = "urlj";
private static final String OFFER = "offerj";
private static final String ENDDATE = "enddatej";

ArrayList<HashMap<String, String>> jsonlist = new ArrayList<HashMap<String, String>>();

ListView lv ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new ProgressTask(Promotions.this).execute();
}

private class ProgressTask extends AsyncTask<String, Void, Boolean> {
    private ProgressDialog dialog;

    private ListActivity activity;

    // private List<Message> messages;
    public ProgressTask(ListActivity activity) {
        this.activity = activity;
        context = activity;
        dialog = new ProgressDialog(context);
    }

    private Context context;

    protected void onPreExecute() {
        this.dialog.setMessage("Progress start");
        this.dialog.show();
    }

    @Override
    protected void onPostExecute(final Boolean success) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
        ListAdapter adapter = new SimpleAdapter(context, jsonlist,
                R.layout.list_item, new String[] { ID, TITLE,
                        URL, OFFER, ENDDATE }, new int[] {
                        R.id.id, R.id.title, R.id.url,
                        R.id.offer, R.id.enddate });

        setListAdapter(adapter);

        // select single ListView item
         lv = getListView();
    }

    protected Boolean doInBackground(final String... args) {

        JSONParser jParser = new JSONParser();

        // get JSON data from URL
        JSONArray json = jParser.getJSONFromUrl(url);

        for (int i = 0; i < json.length(); i++) {

            try {
                JSONObject c = json.getJSONObject(i);
                String idj = c.getString(ID);

                String titlej = c.getString(TITLE);
                String urlj = c.getString(URL);
                String offerj = c.getString(OFFER);
                String enddatej =c.getString(ENDDATE);
                HashMap<String, String> map = new HashMap<String, String>();

                // Add child node to HashMap key & value
                map.put(ID, idj);
                map.put(TITLE, titlej);
                map.put(URL, urlj);
                map.put(OFFER, offerj);
                map.put(ENDDATE, enddatej);
                jsonlist.add(map);
            }
            catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
  }
}

我的logcat就是这个

 02-12 15:52:53.637: E/AndroidRuntime(1005): FATAL EXCEPTION: main
02-12 15:52:53.637: E/AndroidRuntime(1005): Process: com.example.googlemapandroidv2, PID: 1005
02-12 15:52:53.637: E/AndroidRuntime(1005): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlemapandroidv2/com.example.googlemapandroidv2.Mainpage}: java.lang.UnsupportedOperationException: Device does not have package com.google.android.gsf
02-12 15:52:53.637: E/AndroidRuntime(1005):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
02-12 15:52:53.637: E/AndroidRuntime(1005):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
02-12 15:52:53.637: E/AndroidRuntime(1005):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-12 15:52:53.637: E/AndroidRuntime(1005):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-12 15:52:53.637: E/AndroidRuntime(1005):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-12 15:52:53.637: E/AndroidRuntime(1005):     at android.os.Looper.loop(Looper.java:135)
02-12 15:52:53.637: E/AndroidRuntime(1005):     at android.app.ActivityThread.main(ActivityThread.java:5221)
02-12 15:52:53.637: E/AndroidRuntime(1005):     at java.lang.reflect.Method.invoke(Native Method)
02-12 15:52:53.637: E/AndroidRuntime(1005):     at java.lang.reflect.Method.invoke(Method.java:372)
02-12 15:52:53.637: E/AndroidRuntime(1005):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-12 15:52:53.637: E/AndroidRuntime(1005):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-12 15:52:53.637: E/AndroidRuntime(1005): Caused by: java.lang.UnsupportedOperationException: Device does not have package com.google.android.gsf
02-12 15:52:53.637: E/AndroidRuntime(1005):     at com.google.android.gcm.GCMRegistrar.checkDevice(GCMRegistrar.java:98)
02-12 15:52:53.637: E/AndroidRuntime(1005):     at com.example.googlemapandroidv2.Mainpage.onCreate(Mainpage.java:23)
02-12 15:52:53.637: E/AndroidRuntime(1005):     at android.app.Activity.performCreate(Activity.java:5933)
02-12 15:52:53.637: E/AndroidRuntime(1005):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-12 15:52:53.637: E/AndroidRuntime(1005):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
02-12 15:52:53.637: E/AndroidRuntime(1005):     ... 10 more

1 个答案:

答案 0 :(得分:0)

问题是您尝试运行此代码的模拟器或设备未安装Google服务框架(A.K.A Play商店)。

这是导致问题的一行:

GCMRegistrar.checkDevice(this); <--- The root of the crash
GCMRegistrar.checkManifest(this);
GCMRegistrar.register(Mainpage.this,
GCMIntentService.SENDER_ID);

checkDevice方法检查设备是否安装了GSF包。

请参阅stacktrace:

  

引起:java.lang.UnsupportedOperationException:设备没有包com.google.android.gsf

     

在com.google.android.gcm.GCMRegistrar.checkDevice(GCMRegistrar.java:98)

     

在com.example.googlemapandroidv2.Mainpage.onCreate(Mainpage.java:23)

可行的解决方案:

  • 检查Play商店是否已安装;
  • 将Play商店更新为最新版本;
  • 在安装了Play商店的模拟器上运行代码;
  • 确保Play商店应用安装GSF包;