不幸的是app停止工作

时间:2016-01-26 21:05:49

标签: java android

我已附加registeractivity.xml文件,registeractivity.java文件,mainactivity.java文件,清单文件和logcat。

每当我点击注册按钮后输入用户名,电子邮件和密码后运行此应用程序,就会停止工作。

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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.example.user.loginapp.RegisterActivity">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="Username"
        android:ems="10"
        android:id="@+id/usernameRegistereditText"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:hint="E-mail"
        android:id="@+id/emailRegistereditText"
        android:layout_below="@+id/usernameRegistereditText"
        android:layout_centerHorizontal="true" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:hint="Password"
        android:id="@+id/passwordRegistereditText"
        android:layout_below="@+id/emailRegistereditText"
        android:layout_alignParentEnd="true" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Sign Up"
        android:id="@+id/signupbutton"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

RegisterActivity.Java文件:

package com.example.user.loginapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SignUpCallback;

public class RegisterActivity extends AppCompatActivity {

    protected EditText mUsername;
    protected EditText mEmail;
    protected EditText mPassword;
    protected Button mSignup;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        //initialise
        mUsername=(EditText)findViewById(R.id.usernameRegistereditText);
        mEmail=(EditText)findViewById(R.id.emailRegistereditText);
        mPassword=(EditText)findViewById(R.id.passwordRegistereditText);
        mSignup=(Button)findViewById(R.id.signupbutton);

        //Listen to button
        mSignup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //get username password and email and convert to string
                String username=mUsername.getText().toString().trim();
                String email=mEmail.getText().toString().trim();
                String password=mPassword.getText().toString().trim();

                // store user to parse
                ParseUser user=new ParseUser();
                user.setUsername(username);
                user.setEmail(email);
                user.setPassword(password);
                user.signUpInBackground(new SignUpCallback() {
                    @Override
                    public void done(ParseException e) {
                        if(e==null){
                            Toast.makeText(RegisterActivity.this,"Sign-up successful",Toast.LENGTH_LONG).show();
                        }else{
                            //error message
                        }
                    }
                });
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_register, 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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

mainActivity.java文件:

import com.parse.Parse;
import com.parse.ParseObject;
import com.parse.ParseObject.*;

public class MainActivity extends AppCompatActivity {

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

        Parse.initialize(this);
        ParseObject testObject = new ParseObject("TestObject");
        testObject.put("foo", "bar");
        testObject.saveInBackground();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

清单文件:

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.parse.APPLICATION_ID"
            android:value="E9N4CpvflF5WfGVaQ5kBXYHgNA1Z2BE0cXhgc82j" />
        <meta-data
            android:name="com.parse.CLIENT_KEY"
            android:value="IybWtApcNpi8Ky5odgHSAp34oyaxkFw0Jz9voH5U" />

        <activity
            android:name=".RegisterActivity"
            android:label="@string/title_activity_register" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
        </activity>
    </application>

</manifest>

logcat的:

    01-26 16:40:41.028    3836-3836/com.example.user.loginapp E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache
01-26 16:40:41.048    3836-3836/com.example.user.loginapp E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
01-26 16:41:00.912    3836-3836/com.example.user.loginapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.IllegalArgumentException: You must create this type of ParseObject using ParseObject.create() or the proper subclass.
            at com.parse.ParseObject.<init>(ParseObject.java:365)
            at com.parse.ParseObject.<init>(ParseObject.java:334)
            at com.parse.ParseUser.<init>(ParseUser.java:162)
            at com.example.user.loginapp.RegisterActivity$1.onClick(RegisterActivity.java:43)
            at android.view.View.performClick(View.java:4240)
            at android.view.View$PerformClick.run(View.java:17721)
            at android.os.Handler.handleCallback(Handler.java:730)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:1)

替换代码中的以下行

ParseObject testObject = ParseObject.create("TestObject");

用,

provider_id

它将被解决。

Reference

答案 1 :(得分:0)

错误是必须使用create()方法创建ParseObject。您刚刚使用其工厂方法新建了一个。你想要这样的东西:

ParseObject testObject = ParseObject.create("TestObject");

答案 2 :(得分:0)

注释以下几行尝试再次运行:

//  Parse.enableLocalDatastore(this);
    //        Parse.initialize(this);
      //      ParseObject testObject = new ParseObject("TestObject");
        //    testObject.put("foo", "bar");
//            testObject.saveInBackground();