简体   繁体   中英

Can't get what the error is and how to solve it

*i am parsing some data through JSON but when i click on the button to start this parsing activity it stopped working all of the app is working except this button4 *

Main activity of buttons

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



}

Here is the Second activity

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;
    }
  }
}

my logcat is this

 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

The problem is the emulator or device you are trying to run this code on does not have the Google Services Framework installed (AKA Play Store).

This is the line which causes the problem:

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

The checkDevice method checks if the device has the GSF package installed.

See the stacktrace:

Caused by: java.lang.UnsupportedOperationException: Device does not have package com.google.android.gsf

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

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

Posible solutions:

  • Check if the Play Store is installed;
  • Update the Play Store to the newest version;
  • Run the code on a emulator with the Play store installed;
  • Make sure the Play Store app installs the GSF package;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM