使用解析时遇到问题

Trouble in using parse

我正在尝试制作一个使用解析库进行推送的应用程序 notifications.I 按照 parse.com 中的教程进行操作,但它无法正常工作。 我收到错误:

java.lang.RuntimeException: Unable to start activity 

ComponentInfo{com.robotrackerclient/com.robotrackerclient.MainActivity}: 

java.lang.IllegalArgumentException: listener==null

我认为问题出在 code.I 我有一个主 activity 正在启动 app.I activity 我正在把 parse.initialize() main activity.Is 的 onCreate 方法中的代码创建问题?还有一个错误:You must call parse.initialize(..) before using the parse library.

Main Activity 只是一个简单的 class 获取当前位置的纬度和经度并推送它。

         public class MainActivity extends Activity {
         private String imei_code_of_device;
         private LocationManager lm;
         private LocationListener locationListener;
         TelephonyManager mngr;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Specify an Activity to handle all pushes by default.
        //PushService.setDefaultPushCallback(this, MainActivity.class);

        Parse.initialize(this, "APP_KEY", "CLIENT_KEY"); 
        PushService.setDefaultPushCallback(this, MainActivity.class);

        mngr=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
        imei_code_of_device=mngr.getDeviceId();
        lm=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0,0,locationListener);               

        locationListener = new MyLocationListener();
        if ( !lm.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            buildAlertMessageNoGps();
        }
        else{

         lm.requestLocationUpdates(
         LocationManager.GPS_PROVIDER,
         10000,
         0,
         locationListener);
         lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,10000,0,locationListener);              

        }
        }
    private void buildAlertMessageNoGps() {

         final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage("Your GPS seems to be disabled, do you want to enable it?").setCancelable(false).setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                       public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                           startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
                       }
                   })
                   .setNegativeButton("No", new DialogInterface.OnClickListener() {
                       public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
                            dialog.cancel();
                       }
                   });
            final AlertDialog alert = builder.create();
            alert.show();

    }
    private class MyLocationListener implements LocationListener {

        @Override
        public void onLocationChanged(Location loc) {
            if (loc != null) {
                ParseObject po=new ParseObject("LatLang");
                po.put("imei_code",imei_code_of_device);
                po.put("latitude",loc.getLatitude());
                po.put("longitude",loc.getLongitude());
                po.saveInBackground();
                Toast.makeText(getBaseContext(),"Location changed : Lat: " + loc.getLatitude() +" Lng: " + loc.getLongitude(),Toast.LENGTH_SHORT).show();

            }

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub

        }

     }



    @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 boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

这是我的 androidmenifest 文件: 我在 parse.com 上可用的教程的帮助下完成了这些步骤。我包括了 parse.com 教程告诉我要 include.I 的所有内容我没有理解 Application.Because 我必须解析的要点仅在其中一个活动中,它不是第一个 activity launched.So 我不想在我的应用程序启动时初始化解析。

       <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.robotrackerclient"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <permission android:name="com.robotrackerclient.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.robotrackerclient.permission.C2D_MESSAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="com.parse.PushService" />
        <receiver android:name="com.parse.ParseBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>
        <receiver android:name="com.parse.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.robotrackerclient" />
            </intent-filter>
        </receiver>
        <receiver android:name="com.parse.ParsePushBroadcastReceiver"
        android:exported="false">
        <intent-filter>            
        <action android:name="com.parse.push.intent.RECEIVE" />
        <action android:name="com.parse.push.intent.DELETE" />
        <action android:name="com.parse.push.intent.OPEN" />
         </intent-filter>
       </receiver>

       </application>
        </manifest>

您应该在该行中使用您的应用和客户端密钥,而不是默认值 APP_KEYCLIENT_KEYParse.initialize(this, "APP_KEY", "CLIENT_KEY");

在 Parse 注册后即可获得。

好的,非常感谢您的帮助,我能够将推送发送到 parse.com.The 清单中的错误 file.I 更新了 menifestfile 以提供 android:name=."ParseActivity" 在应用程序tag.Then下我在ParseActivityclass中定义了初始化。 现在有人可以告诉如何使用同一组密钥将服务器推送的数据返回到另一台设备吗?