使用解析时遇到麻烦

时间:2015-01-13 18:11:16

标签: android parse-platform

我正在尝试制作一个使用解析库进行推送通知的应用程序。我按照parse.com的教程进行操作,但它不起作用。 我收到了错误:

java.lang.RuntimeException: Unable to start activity 

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

java.lang.IllegalArgumentException: listener==null

我认为问题出在代码中。我正在进行主要活动,即启动app的活动。我将 parse.initialize()代码放在主要活动的onCreate方法中这会造成问题吗?还有一个错误:在使用解析库之前,必须先调用 parse.initialize(..)

主要活动只是一个简单的类,获取当前位置的纬度和经度并推动它。

         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教程说我要包含的所有内容。我没有得到Application.Bec因为我只需解析其中一个活动而且它不是第一个被启动的活动。所以我不想在我的应用程序启动时初始化解析。

       <?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>

2 个答案:

答案 0 :(得分:0)

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

注册Parse后即可获得。

答案 1 :(得分:0)

好的,非常感谢你帮助我能够将push发送到parse.com。错误发生在清单文件中。我通过提供android:name =。&#34; ParseActivity&#34;更新了menifest文件。在应用程序标签下。然后我在ParseActivity类中定义了初始化。 现在,任何人都可以告诉如何使用同一组密钥将服务器上推送的数据放回另一台设备上吗?