2011-05-05 127 views
0
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) 

代码:错误,同时运行在Eclipse中运行时错误的Android项目

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 
     } 

    } 
} 
+1

什么是116? – 2011-05-05 10:15:32

回答

1

你打电话loginDetails()已充气的布局到活动之前。所以,不存在用户界面元素。致电findViewById()之前致电setContentView()

0

你在setContentView(R.layout.view)之前叫CheckBox cb = (CheckBox)findViewById(R.id.remember)。这可能是为什么。 setContentView(R.layout.view)后致电loginDetails()