无法使用Parse添加对象

时间:2014-07-02 20:37:31

标签: android parse-platform

使用最新版本的Eclipse + ADT创建的Android项目,我无法访问parse.com api。如果我使用parse.com预先打包的项目,我可以访问。但是,如果我从头开始创建一个新的Android项目,我就不能。我怀疑问题是因为解析初始化和片段。由于parse.com的预先打包项目不使用片段。在这个程序中,第一个按钮应该向数据库添加一个对象,但这不起作用。任何人都可以知道这个解决方案吗?

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.deneme.MainActivity"
tools:ignore="MergeRootFrame" />

fragment_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.deneme.MainActivity$PlaceholderFragment" >

<LinearLayout
    android:id="@+id/mainLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:orientation="vertical"
    android:paddingBottom="1.0dip"
    android:paddingLeft="4.0dip"
    android:paddingRight="4.0dip"
    android:paddingTop="5.0dip" 
    android:layout_below="@+id/txtTitle">

    <Button
        android:id="@+id/btnCreate"
        android:layout_width="fill_parent"
        android:layout_height="0.0dip"
        android:layout_weight="1.0"
        android:text="Add Object"
        android:textColor="@android:color/white" 
        android:textSize="25sp" />

    <Button
        android:id="@+id/btnIncrement"
        android:layout_width="fill_parent"
        android:layout_height="0.0dip"
        android:layout_weight="1.0"
        android:text="Increment votes"
        android:textColor="@android:color/white"
        android:textSize="25sp" />



</LinearLayout>

的AndroidManifest.xml:

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

<application
    android:name="denemeApp"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.deneme.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>
</application>

denemeApp.java

package com.example.deneme;
import com.parse.Parse;
import com.parse.ParseACL;
import com.parse.ParseUser;
import android.app.Application;
public class denemeApp extends Application {
@Override
public void onCreate() {
    super.onCreate();
    // Add your initialization code here
    Parse.initialize(this, "KEY1", "KEY2");
    ParseUser.enableAutomaticUser();
    ParseACL defaultACL = new ParseACL();
    // If you would like all objects to be private by default, remove this line.
    defaultACL.setPublicReadAccess(true);
    ParseACL.setDefaultACL(defaultACL, true);
}

}

MainActivity.java:

package com.example.deneme;

import com.example.deneme.R;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }


}


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

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);

        Button btnCreate = (Button)rootView.findViewById(R.id.btnCreate);
        btnCreate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ParseObject testObject = new ParseObject("testobject");
            testObject.put("total_votes", 0);
            testObject.saveInBackground();
            }
        });

        Button btnIncrement = (Button)rootView.findViewById(R.id.btnIncrement);
        btnIncrement.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //works with "testobject" and "7erUqKQn1P"
            //doesnt work "testobject2" and "1g68ZP1ikc"
            ParseQuery<ParseObject> query = ParseQuery.getQuery("testobject");

            query.getInBackground("7erUqKQn1P", new GetCallback<ParseObject>()
                {
                    public void done(ParseObject vote, ParseException e)
                    {
                        if(e == null)
                        {
                            vote.increment("total_votes");
                            vote.saveInBackground();
                        }
                    }//done
                }//new GetCallback
            );//query.getInBackground
            }
        });

        return rootView;
    }
}

}

1 个答案:

答案 0 :(得分:0)

您需要将此权限添加到 AndroidManifest.xml 文件

<uses-permission android:name="android.permission.INTERNET" />