java.lang.nullpointerexception:尝试在null对象引用上调用void android.widget.EditText.setText(java.lang.CharSequence)

时间:2015-03-31 12:00:41

标签: java android eclipse

我正在尝试从数据库中检索值,并使用EditText在Android应用程序中显示。但每当我尝试检索它时,项目就会停止。我正在使用eclipse来开发android项目。 Select.java

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); 
?> 

1 个答案:

答案 0 :(得分:1)

我将假设这一行是导致问题的原因:

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

您的activity_select.xml未定义ID为xray的控件,因此对findViewById的调用返回null,并且您对editText3.setText()的调用将导致NPE。但是,它确实具有ID为XRayxRay的TextView。尝试使用其中之一。