2015-03-31 82 views
0

我想從數據庫中檢索值,並使用EditText在android中顯示應用。但是,只要我嘗試檢索它,項目就會停止。我正在使用eclipse來開發android項目。 Select.javajava.lang.nullpointerexception:嘗試在空對象上調用void android.widget.EditText.setText(java.lang.CharSequence)refrence

package com.example.erp_medlabplus; 

import android.annotation.SuppressLint; 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.StrictMode; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
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.json.JSONException; 
import org.json.JSONObject; 

import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 

@SuppressLint("NewApi") 
public class SelectActivity extends Activity { 

    @SuppressLint("NewApi") 
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 

    @SuppressLint("NewApi") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_select); 

     Bundle b = getIntent().getExtras(); 
     EditText ed = (EditText) findViewById(R.id.patient_id); 
     ed.setText(b.getCharSequence("Contents")); 

     Button button = (Button) findViewById(R.id.button1); 
     StrictMode.setThreadPolicy(policy); 

     button.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 
       String result = null; 
       InputStream is = null; 
       EditText editText = (EditText) findViewById(R.id.patient_id); 
       String v1 = editText.getText().toString(); 
       EditText editText1 = (EditText) findViewById(R.id.CT_Scan); 

       EditText editText2 = (EditText) findViewById(R.id.MRI_Scan); 
       EditText editText3 = (EditText) findViewById(R.id.xray); 
       EditText editText4 = (EditText) findViewById(R.id.ECG); 
       EditText editText5 = (EditText) findViewById(R.id.Radiology); 

       ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

       nameValuePairs.add(new BasicNameValuePair("id", v1)); 
       try { 
        HttpClient httpclient = new DefaultHttpClient(); 
        HttpPost httppost = new HttpPost("http://192.168.42.100:8888/ImageUpload/select.php"); 
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
        HttpResponse response = httpclient.execute(httppost); 
        HttpEntity entity = response.getEntity(); 
        is = entity.getContent(); 

        Log.e("log_tag", "connection success "); 
       } 
       catch (Exception e) { 
        Log.e("log_tag", "Error in http connection " + e.toString()); 
        Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show(); 
       } 

       try { 
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
        StringBuilder sb = new StringBuilder(); 
        String line = null; 
        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()); 
        Toast.makeText(getApplicationContext(), " Input reading fail", Toast.LENGTH_SHORT).show(); 
       } 

       //parse json data 
       try { 
        JSONObject object = new JSONObject(result); 
        String ch = object.getString("re"); 
        if (ch.equals("success")) { 
         Toast.makeText(getApplicationContext(), "Retrieval Sucess", Toast.LENGTH_SHORT).show(); 
         JSONObject no = object.getJSONObject("0"); 
         String w = no.getString("CT_Scan"); 
         String w1 = no.getString("MRI_Scan"); 
         String w2 = no.getString("XRay"); 
         String w3 = no.getString("ECG"); 
         String w4 = no.getString("Radiology"); 

         editText1.setText(w); 
         editText2.setText(w1); 
         editText3.setText(w2); 
         editText4.setText(w3); 
         editText5.setText(w4); 
        } else { 
         Toast.makeText(getApplicationContext(), "Record is not available.. Enter valid number", Toast.LENGTH_SHORT).show(); 
        } 
       } 
       catch (JSONException e) { 
        Log.e("log_tag", "Error parsing data " + e.toString()); 
        Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_select, menu); 
     return true; 
    } 

} 

activity_select.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingLeft="16dp" 
    android:paddingRight="16dp" 
    android:paddingTop="16dp" 
    android:paddingBottom="16dp" 
    android:background="@drawable/bg3"> 

<TextView 
    android:id="@+id/header" 
    android:layout_width="match_parent" 
    android:layout_height="30dp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_margin="3dp" 
    android:text="Sample Retrieval" 
    android:gravity="center" 
    android:background="@drawable/bg" 
    android:textColor="#FFFFFF" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<TextView 
    android:id="@+id/patid" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:layout_marginTop="20dp" 
    android:text="Patient ID" 
    android:textColor="#0E52AB" 
    android:layout_below="@+id/header" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:textStyle="bold" 
    /> 

<EditText 
    android:id="@+id/patient_id" 
    android:layout_width="130dp" 
    android:layout_marginTop="20dp" 
    android:layout_marginLeft="20dp" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/header" 
    android:layout_toRightOf="@+id/patid" 
    android:background="@drawable/edittextdesign" 
    android:ems="10" 
    android:numeric="integer" > 

    <requestFocus /> 
</EditText> 

<Button 
    android:id="@+id/button1" 
    android:text="Retrieve" 
    android:radius="14dp" 
    android:layout_below="@+id/patid" 
    android:gravity="center" 
    android:textColor="#FFFFFF" 
    android:textSize="20sp" 
    android:layout_marginTop="20dp" 
    android:layout_width="120dp" 
    android:layout_height="40dp" 
    android:shadowColor="#259FA8" 
    android:shadowDx="0" 
    android:shadowDy="0" 
    android:shadowRadius="5" 
    android:background="@drawable/bg" 
    /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:text="Tests to be taken:" 
    android:textColor="#0E52AB" 
    android:layout_marginTop="20dp" 
    android:id="@+id/test" 
    android:layout_below="@+id/button1" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:textStyle="bold"/> 


<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginTop="20dp" 
    android:layout_below="@+id/test" 
    android:textColor="#0E52AB" 
    android:text="CT Scan" 
    /> 


<EditText 
    android:id="@+id/CT_Scan" 
    android:layout_width="130dp" 
    android:layout_marginTop="10dp" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView1" 
    android:inputType="text" 
    android:ellipsize="start" 
    android:gravity="center" 
    android:background="@drawable/edittextdesign" 
    android:ems="10" 
    /> 

<Button 
    android:id="@+id/CTScan" 
    android:layout_width="80dp" 
    android:layout_height="40dp" 
    android:textColor="#FFFFFF" 
    android:ellipsize="start" 
    android:gravity="center" 
    android:onClick="scan" 
    android:text="Scan" 
    android:background="@drawable/bg" 
    android:layout_above="@+id/textView2" 
    android:layout_toRightOf="@+id/patient_id" 
    android:layout_toEndOf="@+id/patient_id" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_below="@+id/CT_Scan" 
    android:layout_marginTop="10dp" 
    android:text="MRI Scan" 
    android:textColor="#0E52AB"/> 

<EditText 
    android:id="@+id/MRI_Scan" 
    android:layout_width="130dp" 
    android:layout_height="wrap_content" 
    android:inputType="text" 
    android:background="@drawable/edittextdesign" 
    android:layout_marginTop="10dp" 
    android:ellipsize="start" 
    android:gravity="center" 
    android:layout_below="@+id/textView2" 
    android:ems="10" /> 

<Button 
    android:id="@+id/MRIScan" 
    android:layout_width="80dp" 
    android:layout_height="40dp" 
    android:textColor="#FFFFFF" 
    android:ellipsize="start" 
    android:gravity="center" 
    android:onClick="scan" 
    android:text="Scan" 
    android:background="@drawable/bg" 
    android:layout_above="@+id/textView3" 
    android:layout_alignLeft="@+id/CTScan" 
    android:layout_alignStart="@+id/CTScan" /> 

<TextView 
    android:id="@+id/textView3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_below="@+id/MRI_Scan" 
    android:layout_marginTop="10dp" 
    android:text="XRay" 
    android:textColor="#0E52AB"/> 

<EditText 
    android:id="@+id/XRay" 
    android:layout_width="130dp" 
    android:layout_height="wrap_content" 
    android:inputType="text" 
    android:background="@drawable/edittextdesign" 
    android:ellipsize="start" 
    android:gravity="center" 
    android:layout_below="@+id/textView3" 
    android:layout_marginTop="10dp" 
    android:ems="10" /> 

<Button 
    android:id="@+id/xRay" 
    android:layout_width="80dp" 
    android:layout_height="40dp" 
    android:textColor="#FFFFFF" 
    android:ellipsize="start" 
    android:gravity="center" 
    android:onClick="scan" 
    android:text="Scan" 
    android:background="@drawable/bg" 
    android:layout_above="@+id/textView4" 
    android:layout_alignLeft="@+id/MRIScan" 
    android:layout_alignStart="@+id/MRIScan" /> 
<TextView 
    android:id="@+id/textView4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginTop="10dp" 
    android:layout_below="@+id/XRay" 
    android:text="ECG" 
    android:textColor="#0E52AB"/> 

<EditText 
    android:id="@+id/ECG" 
    android:layout_width="130dp" 
    android:layout_height="wrap_content" 
    android:inputType="text" 
    android:background="@drawable/edittextdesign" 
    android:ellipsize="start" 
    android:gravity="center" 
    android:layout_below="@+id/textView4" 
    android:layout_marginTop="10dp" 
    android:ems="10" /> 
<Button 
    android:id="@+id/E_C_G" 
    android:layout_width="80dp" 
    android:layout_height="40dp" 
    android:textColor="#FFFFFF" 
    android:ellipsize="start" 
    android:gravity="center" 
    android:onClick="scan" 
    android:text="Scan" 
    android:background="@drawable/bg" 
    android:layout_above="@+id/textView5" 
    android:layout_alignLeft="@+id/xRay" 
    android:layout_alignStart="@+id/xRay" /> 

<TextView 
    android:id="@+id/textView5" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Radiology" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layout_marginTop="10dp" 
    android:layout_below="@+id/ECG" 
    android:textColor="#0E52AB"/> 

<EditText 
    android:id="@+id/Radiology" 
    android:layout_width="130dp" 
    android:layout_height="wrap_content" 
    android:inputType="text" 
    android:background="@drawable/edittextdesign" 
    android:ellipsize="start" 
    android:gravity="center" 
    android:layout_below="@+id/textView5" 
    android:layout_marginTop="10dp" 
    android:ems="10" /> 

<Button 
    android:id="@+id/radiologyScan" 
    android:layout_width="80dp" 
    android:layout_height="40dp" 
    android:textColor="#FFFFFF" 
    android:ellipsize="start" 
    android:gravity="center" 
    android:onClick="scan" 
    android:text="Scan" 
    android:background="@drawable/bg" 
    android:layout_alignBottom="@+id/Radiology" 
    android:layout_alignLeft="@+id/E_C_G" 
    android:layout_alignStart="@+id/E_C_G" /> 
    </RelativeLayout> 

select.php:

<?php 
    error_reporting(E_ALL^E_DEPRECATED); 
    $con = mysql_connect("localhost","root",""); 

    if (!$con) { 
     die('Could not connect: ' . mysql_error()); 
    } 

    mysql_select_db("erp", $con); 
    $v1=$_REQUEST['id']; 
    //$v1=2; 
    if($v1==NULL) { 
     $r["re"]="Enter the number!!!"; 
     print(json_encode($r)); 
     die('Could not connect: ' . mysql_error()); 
    } 
    else { 
     $i=mysql_query("SELECT * FROM scan_entry_value WHERE id='$v1'",$con); 
     $check=''; 
     //if($i === FALSE) { 
     //die(mysql_error());} // TODO: better error handling 

     while($row = mysql_fetch_array($i)) { 
      //echo $row['CT_Scan']; 
      $r[]=$row; 
      $check=$row['id'];    
     }   

     if($check==NULL) {    
      $r["re"]="Record is not available"; 
      print(json_encode($r)); 
     } 
     else {     
      $r["re"]="success"; 
      print(json_encode($r)); 
     } 
    } 
    mysql_close($con); 
?> 
+2

將錯誤日誌與代碼一起發佈以幫助人們瞭解確切的問題更爲重要。否則,您可能最終失去聲望。 – 2015-03-31 12:03:55

+0

6次調用setText中的哪一個會發生NPE? – 2015-03-31 12:10:20

回答

1

我要承擔這條線是你的問題的原因:

EditText editText3=(EditText)findViewById(R.id.xray); 

Your activi ty_select.xml沒有定義一個ID爲xray的控件,因此對findViewById的調用返回null,並且您對editText3.setText()的調用導致NPE。但是,它具有ID爲XRayxRay的TextView。嘗試使用其中的一種。

+0

糟糕!是我的錯誤。做出改變,現在應用程序完美無缺!萬分感謝 – Amalpriya 2015-03-31 16:44:46

相關問題