2015-09-15 51 views
-1

我需要幫助來找到我的android應用程序的解決方案。試圖登錄到應用程序後,我得到了下面的錯誤。並且應用程序停止運行(強制停止)。 Fyi,我是開發android應用程序的新手。我得到的錯誤:Android java.lang.NullPointerException:println需要一條消息

09-15 00:31:42.602: E/AndroidRuntime(2755): FATAL EXCEPTION: main 
09-15 00:31:42.602: E/AndroidRuntime(2755): Process: com.mobile.learning, PID: 2755 
09-15 00:31:42.602: E/AndroidRuntime(2755): java.lang.NullPointerException: println needs a message 
09-15 00:31:42.602: E/AndroidRuntime(2755):  at android.util.Log.println_native(Native Method) 
09-15 00:31:42.602: E/AndroidRuntime(2755):  at android.util.Log.d(Log.java:139) 
09-15 00:31:42.602: E/AndroidRuntime(2755):  at com.mobile.learning.login$1.onClick(login.java:61) 
09-15 00:31:42.602: E/AndroidRuntime(2755):  at android.view.View.performClick(View.java:4756) 
09-15 00:31:42.602: E/AndroidRuntime(2755):  at android.view.View$PerformClick.run(View.java:19749) 
09-15 00:31:42.602: E/AndroidRuntime(2755):  at android.os.Handler.handleCallback(Handler.java:739) 
09-15 00:31:42.602: E/AndroidRuntime(2755):  at android.os.Handler.dispatchMessage(Handler.java:95) 
09-15 00:31:42.602: E/AndroidRuntime(2755):  at android.os.Looper.loop(Looper.java:135) 
09-15 00:31:42.602: E/AndroidRuntime(2755):  at android.app.ActivityThread.main(ActivityThread.java:5221) 
09-15 00:31:42.602: E/AndroidRuntime(2755):  at java.lang.reflect.Method.invoke(Native Method) 
09-15 00:31:42.602: E/AndroidRuntime(2755):  at java.lang.reflect.Method.invoke(Method.java:372) 
09-15 00:31:42.602: E/AndroidRuntime(2755):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
09-15 00:31:42.602: E/AndroidRuntime(2755):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

這裏是我login.java:

package com.mobile.learning; 

import mobile.config.*; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.HttpURLConnection; 
import java.net.URL; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.os.StrictMode; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class login extends Activity { 
    public koneksi linkurl; 
    String SERVER_URL; 
    private Button login; 
    private EditText username, password; 
    public ProgressDialog progressDialog; 

    //private TextView notif; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.signin); 

     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 

     login = (Button) findViewById(R.id.login); 
     username = (EditText) findViewById(R.id.uname); 
     password = (EditText) findViewById(R.id.passwd); 

     //final TextView notif = (TextView) findViewById(R.id.tv_error); 

     login.setOnClickListener(new OnClickListener() { 

      public void onClick(View v) { 

       // TODO Auto-generated method stub 

       String Re; 
       String mUsername = username.getText().toString(); 
       String mPassword = password.getText().toString(); 

       Re=tryLogin(mUsername, mPassword); 

       Log.d("Check","Here"); 
       Log.d("Re",Re); 
       String temp_check=Re.trim(); 
       if(temp_check.equals("1")) 
       { 
        String nama = username.getText().toString(); 
        Intent newIntent = new Intent(login.this, halamanUtama.class); 
        String txtnama = String.valueOf(nama); 

        //membuat Bundle 
        Bundle bundle = new Bundle(); 

        //menentukan parameter Bundle (id,isi) --> id=nama dan isinya adalah variabel dari txtnama 
        bundle.putString("nama", txtnama); 

        //menambahkan bundle pada intent 
        newIntent.putExtras(bundle); 
        startActivityForResult(newIntent, 0); 
        //notif.setText("SUKSES"); 
       } 
       else 
       { 
        createDialog("Maaf", "Username Atau Password Salah !");     
       }    
      } 
     }); 
    } 
    protected String tryLogin(String mUsername, String mPassword) 
    {   
     Log.d(" TryLoginCheck ","Here"); 
     HttpURLConnection connection; 
     OutputStreamWriter request = null; 

      URL url = null; 
      String response = null; 
      String temp=null; 
      String parameters = "username="+mUsername+"&password="+mPassword; 
      System.out.println("UserName"+mUsername+"\n"+"password"+mPassword); 
      Log.d("Parameters",parameters); 
      try 
      { 
       ; 
       linkurl = new koneksi("/login.php"); 
       SERVER_URL = linkurl.getUrl(); 
       //"http://182.6.232.37:80/ta/login.php" 
       url = new URL(SERVER_URL); 
       connection = (HttpURLConnection) url.openConnection(); 
       connection.setDoOutput(true); 
       connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
       connection.setRequestMethod("POST");  

       request = new OutputStreamWriter(connection.getOutputStream()); 
       request.write(parameters); 
       request.flush(); 
       request.close();    
       String line = "";    
       InputStreamReader isr = new InputStreamReader(connection.getInputStream()); 
       BufferedReader reader = new BufferedReader(isr); 
       StringBuilder sb = new StringBuilder(); 
       while ((line = reader.readLine()) != null) 
       { 

        sb.append(line + "\n"); 
       } 
       temp=sb.toString(); 
       Log.d("Temp",temp); 
       // Response from server after login process will be stored in response variable.     
       response = sb.toString(); 
       Log.d("Response",response); 
       Log.d("Sb Value",sb.toString()); 
       isr.close(); 
       reader.close(); 


      } 
      catch(IOException e) 
      { 
       Toast.makeText(this,e.toString(),0).show(); 
      } 
      // Log.d("Response",response); 
      return response; 
    } 

    private void createDialog(String title, String text) { 
     AlertDialog ad = new AlertDialog.Builder(this) 
     .setPositiveButton("Ok", null) 
     .setTitle(title) 
     .setMessage(text) 
     .create(); 
     ad.show(); 
    } 

} 

回答

0

貌似Re爲空。如果您在tryLogin()中遇到異常,則會發生這種情況。

那就是Log.d("Re",Re);拋出的異樣。 爲什麼試一試Log.d("Re",null);

檢查Re是否爲空bevore如果在tryLogin()中出現異常,則記錄或返回一個空字符串。

1

此代碼引起的錯誤:Log.d(「Re」,Re); ,我認爲由tryLogin函數引起的錯誤/異常使得Re == null。也許你需要在tryLogin中檢查你的代碼。或者最簡單的解決方案改變:

String response = null;
String temp=null;

String response = ""; //or anything string in here String temp=null;

+0

哇感謝它的工作原理! – thenoirlatte