簡體   English   中英

在 eclipse 運行時錯誤中運行 android 項目時出錯

[英]Error while running the android project in eclipse run time error

05-05 14:32:25.210: ERROR/AndroidRuntime(551): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.org.pc/com.org.pc.Login}: java.lang.NullPointerException
05-05 14:32:25.210: ERROR/AndroidRuntime(551):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-05 14:32:25.210: ERROR/AndroidRuntime(551):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-05 14:32:25.210: ERROR/AndroidRuntime(551):     at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-05 14:32:25.210: ERROR/AndroidRuntime(551):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-05 14:32:25.210: ERROR/AndroidRuntime(551):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-05 14:32:25.210: ERROR/AndroidRuntime(551):     at android.os.Looper.loop(Looper.java:123)
05-05 14:32:25.210: ERROR/AndroidRuntime(551):     at android.app.ActivityThread.main(ActivityThread.java:4363)
05-05 14:32:25.210: ERROR/AndroidRuntime(551):     at java.lang.reflect.Method.invokeNative(Native Method)
05-05 14:32:25.210: ERROR/AndroidRuntime(551):     at java.lang.reflect.Method.invoke(Method.java:521)
05-05 14:32:25.210: ERROR/AndroidRuntime(551):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-05 14:32:25.210: ERROR/AndroidRuntime(551):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-05 14:32:25.210: ERROR/AndroidRuntime(551):     at dalvik.system.NativeStart.main(Native Method)
05-05 14:32:25.210: ERROR/AndroidRuntime(551): Caused by: java.lang.NullPointerException
05-05 14:32:25.210: ERROR/AndroidRuntime(551):     at com.org.pc.Login.loginDetails(Login.java:116)
05-05 14:32:25.210: ERROR/AndroidRuntime(551):     at com.org.pc.Login.onCreate(Login.java:54)
05-05 14:32:25.210: ERROR/AndroidRuntime(551):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-05 14:32:25.210: ERROR/AndroidRuntime(551):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)

代碼是:

package com.org.pc;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;



import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;


public class Login extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);   
        Log.i("Login","Reached");

        Log.i("Login","View set");

        Bitmap bitmap = (Bitmap) this.getIntent().getParcelableExtra("BitmapImage");
        if(bitmap==null)
            Log.v("Sdrawable","null");
        else
            Log.v("Sdrawable","not null");
        loginDetails();
        setContentView(R.layout.view);
        ImageView viewBitmap = (ImageView)findViewById(R.id.bitmapview);
        viewBitmap.setImageBitmap(bitmap);
        Button login=(Button) findViewById(R.id.login);
        SharedPreferences pref = getSharedPreferences(PREFS_NAME,MODE_PRIVATE);   
        username = pref.getString(PREF_USERNAME, null);
        password = pref.getString(PREF_PASSWORD, null);
        if(username != null && password != null) 
        {
            Log.v("username",username);
            //user.setText(username);
            //pass.setText(password);
        }

         login.setOnClickListener(new Button.OnClickListener() 
         { 
             public void onClick (View v)
             { 
                 setContentView(R.layout.home); 
             }
             });




    }

    public static final String PREFS_NAME = "PrefsFile";
    private static final String PREF_USERNAME = "username";
    private static final String PREF_PASSWORD = "password";
    public String username = null;
    public String password = null;
    //EditText user = (EditText)findViewById(R.id.username);
    //EditText pass = (EditText)findViewById(R.id.password);
    public void loginDetails()
    {

        if (username == null || password == null) 
        {
        //  username = user.getText().toString();
        //  password = pass.getText().toString();
        }

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://69.10.60.88:88/BZLogin.aspx");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", "abdul"));
            nameValuePairs.add(new BasicNameValuePair("password", "abdul"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity resEntity = response.getEntity();  
            if (resEntity != null) {    
                Log.i("RESPONSE",EntityUtils.toString(resEntity));
            }
            CheckBox cb = (CheckBox)findViewById(R.id.remember);

                    if ( cb.isChecked() )
                    {

                        getSharedPreferences(PREFS_NAME,MODE_PRIVATE)
                        .edit()
                        .putString(PREF_USERNAME, username)
                        .putString(PREF_PASSWORD, password)
                        .commit();
                    }




        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }

    }
}

在將布局膨脹到活動之前,您正在調用 loginDetails()。 因此,不存在 ui 元素。 在調用findViewById() setContentView() ) 。

您在setContentView(R.layout.view)之前調用CheckBox cb = (CheckBox)findViewById(R.id.remember) ) 。 這可能就是為什么。 setContentView(R.layout.view)之后調用loginDetails() ) 。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM