开发者

Android simple login screen crashes app when logging, no errors in eclipse. Which java class activity is at fault?

package com.android.drinksonme;

import android.app.Activity;

import android.content.Intent;
import android.util.Log;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Screen2 extends Activity {

    // Declare our Views, so we can access them later
    private EditText etUsername;
    private EditText etPassword;
    private Button btnLogin;
    //private Button btnSignUp;
    private TextView lblResult;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Get the EditText and Button References
                etUsername = (EditText)findViewById(R.id.username);
                etPassword = (EditText)findViewById(R.id.password);
                btnLogin = (Button)findViewById(R.id.login_button);
               // btnSignUp = (Button)findViewById(R.id.signup_button);
                lblResult = (TextView)findViewById(R.id.result);

                       btnLogin.setOnClickListener(new OnClickListener() {
                           @Override
                           public void onClick(View v) {
                            String username = etUsername.getText().toString();
                            String password = etPassword.getText().toString();

                           if(username.equals("test") && password.equals("test")){
                               Intent i = new Intent(Screen2.this, DrinksTwitter.class);
                               startActivity(i);
                           } else {
                               lblResult.setText("Invalid username or password."); 
                           } 
                         }
                       });
    }
}
                         /*  final Intent k = new Intent(Screen2.this, SignUp.class);
                           btnSignUp.setOnClickListener(new OnClickListener() {
                               public void onClick(View v) { 
                                   startActivity(k);
                               }*/

This is the log trace:

12-29 17:55:50.215: WARN/KeyCharacterMap(276): No keyboard for id 0
12-29 17:55:50.215: WARN/KeyCharacterMap(276): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-29 17:55:56.645: DEBUG/dalvikvm(120): GC_FOR_MALLOC freed 1585 objects / 216672 bytes in 77ms
12-29 17:55:57.615: DEBUG/dalvikvm(120): GC_EXTERNAL_ALLOC freed 1934 objects / 132912 bytes in 74ms
12-29 17:55:59.136: WARN/KeyCharacterMap(120): No keyboard for id 0
12-29 17:55:59.165: WARN/KeyCharacterMap(120): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
12-29 17:56:03.435: INFO/ActivityManager(58): Starting activity: Intent { cmp=com.android.drinksonme/.DrinksTwitter }
12-29 17:56:03.965: DEBUG/AndroidRuntime(276): Shutting down VM
12-29 17:56:03.965: WARN/dalvikvm(276): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-29 17:56:04.335: ERROR/AndroidRuntime(276): FATAL EXCEPTION: main
12-29 17:56:04.335: ERROR/AndroidRuntime(276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.drinksonme/com.android.drinksonme.DrinksTwitter}: java.lang.NullPointerException
12-29 17:56:04.335: ERROR/AndroidRuntime(276):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
12-29 17:56:04.335: ERROR/AndroidRuntime(276):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
12-29 17:56:04.335: ERROR/AndroidRuntime(276):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
12-29 17:56:04.335: ERROR/AndroidRuntime(276):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
12-29 17:56:04.335: ERROR/AndroidRuntime(276):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 17:56:04.335: ERROR/AndroidRuntime(276):     at android.os.Looper.loop(Looper.java:123)
12-29 17:56:04.335: ERROR/AndroidRuntime(276):     at android.app.ActivityThread.main(ActivityThread.java:4627)
12-29 17:56:04.335: ERROR/AndroidRuntime(276):     at java.lang.reflect.Method.invokeNative(Native Method)
12-29 17:56:04.335: ERROR/AndroidRuntime(276):     at java.lang.reflect.Method.invoke(Method.java:521)
12-29 17:56:04.335: ERROR/AndroidRuntime(276):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-29 17:56:04.335: ERROR/AndroidRuntime(276):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-29 17:56:04.335: ERROR/AndroidRuntime(276):     at dalvik.system.NativeStart.main(Native Method)
12-29 17:56:04.335: ERROR/AndroidRuntime(276): Caused by: java.lang.NullPointerException
12-29 17:56:04.335: ERROR/AndroidRuntime(276):     at com.android.drinksonme.DrinksTwitter.onCreate(DrinksTwitter.java:24)
12-29 17:56:04.335: ERROR/AndroidRuntime(276):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
12-29 17:56:04.335: ERROR/AndroidRuntime(276):开发者_运维问答     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
12-29 17:56:04.335: ERROR/AndroidRuntime(276):     ... 11 more
12-29 17:56:04.415: WARN/ActivityManager(58):   Force finishing activity com.android.drinksonme/.DrinksTwitter
12-29 17:56:04.435: WARN/ActivityManager(58):   Force finishing activity com.android.drinksonme/.Screen2
12-29 17:56:04.975: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{43f9f0c8 com.android.drinksonme/.DrinksTwitter}
12-29 17:56:05.045: INFO/ARMAssembler(58): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x340c68:0x340d24] in 4299931 ns
12-29 17:56:15.909: WARN/ActivityManager(58): Activity destroy timeout for HistoryRecord{43f470c8 com.android.drinksonme/.Screen2}
12-29 17:56:15.909: WARN/ActivityManager(58): Activity destroy timeout for HistoryRecord{43f9f0c8 com.android.drinksonme/.DrinksTwitter}

Snippet from DrinksTwitter.java: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.specials);

        WebView mWebView = (WebView) findViewById(R.id.drinks);

/line:24/ mWebView.getSettings().setJavaScriptEnabled(true); mWebView.loadUrl("http://www.yahoo.com"); }


12-29 17:56:04.335: ERROR/AndroidRuntime(276): Caused by: java.lang.NullPointerException
12-29 17:56:04.335: ERROR/AndroidRuntime(276):     at com.android.drinksonme.DrinksTwitter.onCreate(DrinksTwitter.java:24)

This is where the NullPointerException is being thrown that's crashing your app. What's line 24 of DrinksTwitter.java?


Try replacing setContentView(R.layout.main) with setContentView(R.layout.activity_screen2). Or there might some kind of error in your DrinksTwitter activity.


You should try to validate the WebView then call it's methods

if(mWebView != null){
    mWebView.getSettings().setJavaScriptEnabled(true); 
    mWebView.loadUrl("http://www.yahoo.com"); 
}

Also make sure that R.id.drinks is present on your layout file and that the id is correct

    <WebView android:id="@+id/drinks"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜