调用意图时应用程序关闭

时间:2015-08-17 19:04:09

标签: java android class android-intent

我有2节课。我从我的菜单类调用一个intent,当我调用intent到我的MainActivity类时,它给了我一个错误,上面写着,不幸的是,app name已经停止了。我需要帮助搞清楚发生了什么。我知道这是因为MainActivity中存在错误。我只是需要帮助找到它。

这是MainActivity.java的代码:

package com.arborhillsvet.arborhillsveterinarianclinic;
import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ViewAnimator;

import com.parse.FindCallback;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParsePush;
import com.parse.ParseQuery;
import com.parse.SaveCallback;

import java.util.List;

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, "parse information", "parse information");

    ParsePush.subscribeInBackground("", new SaveCallback() {
        @Override
        public void done(ParseException e) {
            if (e == null) {
                Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
            } else {
                Log.e("com.parse.push", "failed to subscribe for push", e);
            }
        }
    });

    WebView appointWeb = (WebView) findViewById(R.id.appointments);
    appointWeb.loadUrl("https://www.google.com");
    WebSettings webSettings = appointWeb.getSettings();
    webSettings.setJavaScriptEnabled(true);
    appointWeb.setWebViewClient(new WebViewClient());

    WebView promosWeb = (WebView) findViewById(R.id.promos);
    promosWeb.loadUrl("https://www.yahoo.com");
    WebSettings webSettings2 = promosWeb.getSettings();
    webSettings2.setJavaScriptEnabled(true);
    promosWeb.setWebViewClient(new WebViewClient());

    WebView infoWeb = (WebView) findViewById(R.id.info);
    infoWeb.loadUrl("https://www.bing.com");
    WebSettings webSettings3 = infoWeb.getSettings();
    webSettings3.setJavaScriptEnabled(true);
    infoWeb.setWebViewClient(new WebViewClient());

    WebView medsWeb = (WebView) findViewById(R.id.medicines);
    medsWeb.loadUrl("https://www.aol.com");
    WebSettings webSettings4 = medsWeb.getSettings();
    webSettings4.setJavaScriptEnabled(true);
    medsWeb.setWebViewClient(new WebViewClient());




    Button next = (Button) findViewById(R.id.next);
    View.OnClickListener listener = new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            ViewAnimator anim = (ViewAnimator) findViewById(R.id.views);
            anim.showNext();
        }

    };

    next.setOnClickListener(listener);

    Button prev = (Button) findViewById(R.id.prev);
    View.OnClickListener listener2 = new View.OnClickListener() {

        public void onClick(View view) {


            ViewAnimator anim = (ViewAnimator) findViewById(R.id.views);
            anim.showPrevious();

        }


    };

    prev.setOnClickListener(listener2);

    ImageButton menu = (ImageButton) findViewById(R.id.menu);
    View.OnClickListener listener3 = new View.OnClickListener() {

        public void onClick(View view) {
    Intent intent = new Intent(MainActivity.this, menu.class);
    startActivity(intent);
        }


    };

    menu.setOnClickListener(listener3);

}



}

我也编辑过这篇文章,并在有人要求之后添加了Logcat。我想我看到了错误,看起来好像是Parse.initialize和Parse.enableLocalDatastore方法有问题,虽然我没有看到它们的位置有什么问题。我想知道我是否需要让线程休眠几秒钟,以便本地数据存储可以先加载。我不确定,但这里是Logcat。

>08-17 19:39:03.018    4601-4601/com.arborhillsvet.arborhillsveterinarianclinic E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.arborhillsvet.arborhillsveterinarianclinic, PID: 4601
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.arborhillsvet.arborhillsveterinarianclinic/com.arborhillsvet.arborhillsveterinarianclinic.MainActivity}: java.lang.IllegalStateException: `Parse#enableLocalDatastore(Context)` must be invoked before `Parse#initialize(Context)`
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.IllegalStateException: `Parse#enableLocalDatastore(Context)` must be invoked before `Parse#initialize(Context)`
            at com.parse.Parse.enableLocalDatastore(Parse.java:58)
            at com.arborhillsvet.arborhillsveterinarianclinic.MainActivity.onCreate(MainActivity.java:36)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

我需要帮助!拜托,谢谢你。

1 个答案:

答案 0 :(得分:0)

我想您必须在Application课程中初始化可能会出现问题,因为您在MainActivity初始化

这样做

public class ParseApplication extends Application {

@Override
public void onCreate() {
    super.onCreate();

    ParseCrashReporting.enable(this);
    Parse.enableLocalDatastore(this);
    Parse.initialize(this, "xxx", "xxx");
}
}