-1

と の 2 つのアクティビティがMainpage.javaありSecondary.javaます。そして、両方のクラスから同じphpファイルにアクセスしようとしています。2 番目のクラスが呼び出されると、次のエラーが取り消されます。

  java.lang.nullpointerexception.

このエラーを削除するにはどうすればよいですか?

これにより、アプリケーションが強制的に閉じられます。ありがとう。これは最初のクラスのコードです: void login(){ try{

       httpclient=new DefaultHttpClient();

       httppost= new HttpPost("http://10.0.2.2/ABC/login.php"); 
       nameValuePairs = new ArrayList<NameValuePair>(2);
       nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim()));  
       nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim()));
       nameValuePairs.add(new BasicNameValuePair("action","LOGIN"));

       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

       ResponseHandler<String> responseHandler = new BasicResponseHandler();
       response=httpclient.execute(httppost);
       final String response = httpclient.execute(httppost, responseHandler);
       System.out.println("Response : " + response);


               tv.setText("Response from PHP : " + response);

               if(response.equalsIgnoreCase("User Found"))
               {
                   Toast.makeText(Axdroid.this,"Login Success",Toast.LENGTH_SHORT).show();   

                   startActivity(new Intent(getApplicationContext(),Secondary.class));
               }                   else
               {
                    showAlert();     
               }                                     

       dialog.dismiss();   


   }catch(Exception e){

       System.out.println("Exception : " + e.getMessage());
   }

}

そして、これは二次クラスの void jsr(){

    try{
        httpclient = new DefaultHttpClient();
        httppost= new HttpPost("http://10.0.2.2/AndroidLeave/login.php");
        nameValuePairs.add(new BasicNameValuePair("action","SUMMARY"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


        response = httpclient.execute(httppost);

        System.out.println(" ++++++++  +++++++++++++"+response);                    

        entity = response.getEntity();
        is = entity.getContent();
    }catch(Exception e){
        Log.e("log_tag", "Error in http connection"+e.toString());
    }

    System.out.println(" ++++++++ After getting data from PHP file +++++++++++++");

    //convert response to string
    try{
        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
        sb = new StringBuilder();
        sb.append(reader.readLine() + "\n");
        String line="0";

        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }

        is.close();
        result=sb.toString();

    }catch(Exception e){
        Log.e("log_tag", "Error converting result "+e.toString());}
    }}

ログキャット:

 10-26 18:10:01.313: W/SingleClientConnManager(2296): Make sure to release the connection before allocating another one.
 10-26 18:10:01.964: E/log_tag(2296): Error in http connectionjava.lang.NullPointerException
 10-26 18:10:01.974: E/log_tag(2296): Error converting result java.lang.NullPointerException
4

2 に答える 2

0

2番目の接続を開始しようとしたが、最初の接続を閉じなかったためにエラーが発生します。InputStream最初のクラスでは使用しなかったので、2番目のクラスのように電話をかけませんでしたis.close()

ファーストクラスで、に2回電話をかける理由がよくわかりませんexecute()

あなたはこれを行うことができます:これらの2行を削除します

ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);

そしてこの行の後

response=httpclient.execute(httppost);

追加:

final String responseString = EntityUtils.toString(response.getEntity());

次に、として使用するresponseString場所で使用responseしますString。それでも発生する場合は、その後にEntityUtils.consume(response.getEntity());を追加します。shoudも消費しますが、toStringそうでない場合に備えて。

それでも機能しない場合は、お知らせください。

于 2012-10-26T14:00:50.700 に答える
0

コードをすばやく確認すると、2番目のクラスのこの行で例外が発生している可能性があります。

nameValuePairs.add(new BasicNameValuePair("action","SUMMARY"));

nameValuePairsnullだからです。次のようなものを追加してみてください。

nameValuePairs = new ArrayList<NameValuePair>(2);

電話をかける前にnameValuePairs.add()

于 2012-10-26T14:01:54.603 に答える