为什么startActivity会导致崩溃?

时间:2014-02-27 17:31:58

标签: android

谢谢你们!原因在于比我想象的更简单。我觉得有些无用..: - )

我已经在androidmanifest.xml上添加了活动名称。 我的代码很简单。

        Intent intent=new Intent(MainActivity.this, AddAddressActivity.class);
        intent.putExtra("respond", responsebody);
        intent.putExtra("userid", txtID.getText().toString());
        intent.putExtra("password", txtPW.getText().toString());
        MainActivity.this.startActivity(intent);    

就是这个。

为什么在此代码中导致崩溃?我找不到它。 但更奇怪的是,当我将AddAddressActivity作为基本的模板(当它最初制作时的源)时,不会导致崩溃。 那么,AddAddressActivity是一个问题吗?

感谢您阅读我的问题。因为我的英语短暂,你可能会误解它。如果你有难以理解的东西,请在评论时问我。

这是Logcat。我在这篇文章中找不到理由..

02-28 02:42:53.876: D/dalvikvm(6195): GC_FOR_ALLOC freed 122K, 19% free 9983K/12192K, paused 17ms, total 17ms
02-28 02:42:53.886: I/dalvikvm-heap(6195): Grow heap (frag case) to 15.536MB for 3936016-byte allocation
02-28 02:42:53.901: D/dalvikvm(6195): GC_FOR_ALLOC freed <1K, 14% free 13826K/16036K, paused 13ms, total 13ms
02-28 02:42:54.011: D/dalvikvm(6195): GC_FOR_ALLOC freed <1K, 14% free 13826K/16036K, paused 9ms, total 9ms
02-28 02:42:54.031: I/dalvikvm-heap(6195): Grow heap (frag case) to 30.549MB for 15744016-byte allocation
02-28 02:42:54.041: D/dalvikvm(6195): GC_FOR_ALLOC freed 0K, 8% free 29201K/31412K, paused 11ms, total 11ms
02-28 02:42:54.201: D/libEGL(6195): loaded /system/lib/egl/libEGL_mali.so
02-28 02:42:54.201: D/libEGL(6195): loaded /system/lib/egl/libGLESv1_CM_mali.so
02-28 02:42:54.206: D/libEGL(6195): loaded /system/lib/egl/libGLESv2_mali.so
02-28 02:42:54.211: E/(6195): Device driver API match
02-28 02:42:54.211: E/(6195): Device driver API version: 23
02-28 02:42:54.211: E/(6195): User space API version: 23 
02-28 02:42:54.211: E/(6195): mali: REVISION=Linux-r3p2-01rel3 BUILD_DATE=Wed Oct 30 09:36:10 KST 2013 
02-28 02:42:54.276: D/OpenGLRenderer(6195): Enabling debug mode 0
02-28 02:43:02.696: I/webclipboard(6195): clipservice: android.sec.clipboard.ClipboardExManager@4419e048
02-28 02:43:02.711: D/AndroidRuntime(6195): Shutting down VM
02-28 02:43:02.711: W/dalvikvm(6195): threadid=1: thread exiting with uncaught exception (group=0x420ac700)
02-28 02:43:02.721: E/AndroidRuntime(6195): FATAL EXCEPTION: main
02-28 02:43:02.721: E/AndroidRuntime(6195): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mcdelivery/com.example.mcdelivery.AddAddressActivity}: java.lang.NullPointerException
02-28 02:43:02.721: E/AndroidRuntime(6195):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
02-28 02:43:02.721: E/AndroidRuntime(6195):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2362)
02-28 02:43:02.721: E/AndroidRuntime(6195):     at android.app.ActivityThread.access$700(ActivityThread.java:168)
02-28 02:43:02.721: E/AndroidRuntime(6195):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
02-28 02:43:02.721: E/AndroidRuntime(6195):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 02:43:02.721: E/AndroidRuntime(6195):     at android.os.Looper.loop(Looper.java:137)
02-28 02:43:02.721: E/AndroidRuntime(6195):     at android.app.ActivityThread.main(ActivityThread.java:5493)
02-28 02:43:02.721: E/AndroidRuntime(6195):     at java.lang.reflect.Method.invokeNative(Native Method)
02-28 02:43:02.721: E/AndroidRuntime(6195):     at java.lang.reflect.Method.invoke(Method.java:525)
02-28 02:43:02.721: E/AndroidRuntime(6195):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
02-28 02:43:02.721: E/AndroidRuntime(6195):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
02-28 02:43:02.721: E/AndroidRuntime(6195):     at dalvik.system.NativeStart.main(Native Method)
02-28 02:43:02.721: E/AndroidRuntime(6195): Caused by: java.lang.NullPointerException
02-28 02:43:02.721: E/AndroidRuntime(6195):     at com.example.mcdelivery.AddAddressActivity.onCreate(AddAddressActivity.java:18)
02-28 02:43:02.721: E/AndroidRuntime(6195):     at android.app.Activity.performCreate(Activity.java:5372)
02-28 02:43:02.721: E/AndroidRuntime(6195):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
02-28 02:43:02.721: E/AndroidRuntime(6195):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2270)
02-28 02:43:02.721: E/AndroidRuntime(6195):     ... 11 more
02-28 02:43:02.751: V/webkit(6195): BrowserFrame constructor: this=Handler (android.webkit.BrowserFrame) {4419b1f0}

AddAddressActivity.java

   package com.example.mcdelivery;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebView;

public class AddAddressActivity extends Activity {
    WebView webview;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addaddress);

        Intent intent=new Intent(this.getIntent());
        webview.loadUrl("https://www.mcdelivery.co.kr/kr/jsp/sys/customerLogin.do?userName=" + intent.getStringExtra("username") + "&password=" + intent.getStringExtra("password") + "&imgLogin.x=0&imgLogin.y=0");
    }

}

3 个答案:

答案 0 :(得分:1)

webView在此行中提供NPE

webview.loadUrl("https://www.mcdelivery.co.kr/kr/jsp/sys/customerLogin.do?userName=" + intent.getStringExtra("username") + "&password=" + intent.getStringExtra("password") + "&imgLogin.x=0&imgLogin.y=0");

因为您还没有初始化它。你只声明它

WebView webview;

某处(在该行之前和setContentView()之后),您需要初始化它

答案 1 :(得分:0)

我认为这是对属于您的应用的上下文的错误处理。

替换

Intent intent=new Intent(MainActivity.this, AddAddressActivity.class);

Intent intent=new Intent(this, AddAddressActivity.class);

答案 2 :(得分:0)

您只声明WebView未初始化

setContentView(R.layout.activity_addaddress);
webview =(WebView)findViewById(R.id.webView); //missing
Intent intent= getIntent();
webview.loadUrl("https://www.mcdelivery.co.kr/kr/jsp/sys/customerLogin.do?userName=" + intent.getStringExtra("username") + "&password=" + intent.getStringExtra("password") + "&imgLogin.x=0&imgLogin.y=0");