2015-04-06 41 views
-2

我是新來開發一個android應用程序。我已經閱讀了很多關於我所問的問題的相關文章,但帖子的提示或解決方案並未解決我的問題。 (尋找了一個星期已經解決,真正需要幫助才能繼續進行我的項目)非常感謝...
錯誤線121號 Image LinK轉換http實體結果時出錯java.lang.NullPointerException:lock == null

update.java:

 package pounkumarpurushothaman.ghssvmm; 

    import android.app.Activity; 
     import android.support.v7.app.ActionBarActivity; 
    import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
import android.widget.Spinner; 
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.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 


public class update extends ActionBarActivity { 
public static final String KEY_121 ="http://ghssvmm.site40.net/update.php"; 
static Activity thisActivity = null; 
InputStream is = null; 
// LinearLayout yy=(LinearLayout)findViewById(R.id.uy); 
// LinearLayout pp=(LinearLayout)findViewById(R.id.up); 
//LinearLayout cc=(LinearLayout)findViewById(R.id.uc); 
//LinearLayout ca=(LinearLayout)findViewById(R.id.uca); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_update); 
    getServerData(); 
} 


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

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
private void getServerData() { 
    thisActivity=this; 
    EditText f = (EditText) findViewById(R.id.ufst); 
    EditText l = (EditText) findViewById(R.id.ulst); 
    Spinner bld=(Spinner)findViewById(R.id.ubld); 
    Spinner stat=(Spinner)findViewById(R.id.ustatus); 
    Spinner bth=(Spinner)findViewById(R.id.ubat); 
    EditText dob = (EditText) findViewById(R.id.udob); 
    EditText phn1 = (EditText) findViewById(R.id.uphn); 
    EditText phn2 = (EditText) findViewById(R.id.uphn2); 
    EditText padd = (EditText) findViewById(R.id.upadd); 
    EditText radd = (EditText) findViewById(R.id.uradd); 
    EditText vill = (EditText) findViewById(R.id.uvill); 
    Spinner sec=(Spinner)findViewById(R.id.usec); 
    Spinner deg=(Spinner)findViewById(R.id.udeg); 
    EditText dept = (EditText) findViewById(R.id.udept); 
    EditText clg = (EditText) findViewById(R.id.uclg); 
    EditText yop = (EditText) findViewById(R.id.uyop); 
    EditText pos = (EditText) findViewById(R.id.upos); 
    EditText cmp = (EditText) findViewById(R.id.ucom); 
    EditText cadd = (EditText) findViewById(R.id.ucaddr); 


    String result = ""; 
    //the year data to send 
    final ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs.add(new BasicNameValuePair("email","[email protected]")); 
    Thread thread = new Thread(new Runnable(){ 
     @Override 
     public void run() { 
      try { 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost(KEY_121); 
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 
      } catch (Exception e) { 
       Toast.makeText(thisActivity, "Error in http connection " + e.toString(),Toast.LENGTH_LONG).show(); 
      } 
     } 
    }); 

    thread.start(); 




    //convert response to string 
    try{ 

     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) { 
      sb.append(line+"n"); 
     } 
     is.close(); 
     result=sb.toString(); 
    }catch(Exception e){ 
     Toast.makeText(this, "Error converting result "+e.toString(),Toast.LENGTH_LONG).show(); 
    } 
    //parse json data 
    try{ 
     JSONArray jArray = new JSONArray(result); 
     for(int i=0;i<jArray.length();i++){ 
      JSONObject json_data = jArray.getJSONObject(i); 
      f.setText(json_data.getString("fname")); 
      l.setText(json_data.getString("lname")); 
      dob.setText(json_data.getString("dob")); 
      switch(json_data.getString("blood")){ 
       case "O Positive": 
        bld.setSelection(1); 
        break; 
       case "O Negative": 
        bld.setSelection(2); 
        break; 
       case "A Positive": 
        bld.setSelection(3); 
        break; 
       case "A Negative": 
        bld.setSelection(4); 
        break; 
       case "B Positive": 
        bld.setSelection(5); 
        break; 
       case "B Negative": 
        bld.setSelection(6); 
        break; 
       case "AB Positive": 
        bld.setSelection(7); 
        break; 
       case "AB Negative": 
        bld.setSelection(8); 
        break; 
      } 
      switch(json_data.getString("section")) { 
       case "A": 
        sec.setSelection(1); 
        break; 
       case "B": 
        sec.setSelection(2); 
        break; 
       case "C": 
        sec.setSelection(3); 
        break; 
      } 
      phn1.setText(json_data.getString("phn")); 
      phn2.setText(json_data.getString("phn2")); 
      padd.setText(json_data.getString("add")); 
      radd.setText(json_data.getString("add2")); 
      switch(json_data.getString("status")) { 
       case "Student": 
        stat.setSelection(1); 
        /* cc.setVisibility(View.INVISIBLE); 
        yy.setVisibility(View.INVISIBLE); 
        pp.setVisibility(View.INVISIBLE); 
        ca.setVisibility(View.INVISIBLE);*/ 
        break; 
       case "Employee": 
        stat.setSelection(2); 
        /*cc.setVisibility(View.VISIBLE); 
        yy.setVisibility(View.VISIBLE); 
        pp.setVisibility(View.VISIBLE); 
        ca.setVisibility(View.VISIBLE);*/ 
        break; 
       case "Un Employee": 
        stat.setSelection(3); 
        /* cc.setVisibility(View.INVISIBLE); 
        yy.setVisibility(View.VISIBLE); 
        pp.setVisibility(View.INVISIBLE); 
        ca.setVisibility(View.INVISIBLE);*/ 
        break; 
      } 
      switch(json_data.getString("degree")) { 
       case "B.E": 
        deg.setSelection(1); 
        break; 
       case "Arts and Science": 
        deg.setSelection(2); 
        break; 
       case "Medical": 
        deg.setSelection(3); 
        break; 
      } 
      dept.setText(json_data.getString("dept")); 
      clg.setText(json_data.getString("clg")); 
      yop.setText(json_data.getString("yop")); 
      pos.setText(json_data.getString("pos")); 
      cmp.setText(json_data.getString("com")); 
      cadd.setText(json_data.getString("caddr")); 
      vill.setText(json_data.getString("vill")); 
      //Get an output to the screen 

     } 
    }catch(JSONException e) { 
     Toast.makeText(this, "Error parsing data " + e.toString(), Toast.LENGTH_LONG).show(); 
    } 

} 

}

**

的logcat: **

04-06 14:11:39.580 25919-25919/? E/AndroidRuntime﹕ FATAL EXCEPTION: main 
Process: pounkumarpurushothaman.ghssvmm, PID: 25919 
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{pounkumarpurushothaman.ghssvmm/pounkumarpurushothaman.ghssvmm.update}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388) 
     at android.app.ActivityThread.access$800(ActivityThread.java:148) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5312) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference 
     at android.app.Activity.findViewById(Activity.java:2081) 
     at pounkumarpurushothaman.ghssvmm.update.<init>(update.java:34) 
     at java.lang.reflect.Constructor.newInstance(Native Method) 
     at java.lang.Class.newInstance(Class.java:1572) 
     at android.app.Instrumentation.newActivity(Instrumentation.java:1088) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2215) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2388) 
     at android.app.ActivityThread.access$800(ActivityThread.java:148) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1292) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5312) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 

+1

您需要發佈您的代碼和錯誤日誌。 – Chris 2015-04-06 13:30:41

+0

其中你得到的錯誤行是 – Prashant 2015-04-06 13:42:22

+0

至少提供一個堆棧跟蹤,它會給出行號。你給我們的東西是「這本詞典有拼寫錯誤的單詞」,然後不告訴我們哪個單詞拼寫錯誤。 – Russ 2015-04-06 13:53:37

回答

0

下面是您執行POST請求後的響應方式。

    HttpResponse response = httpClient.execute(httpPost); 
        String op = EntityUtils.toString(response.getEntity(), "UTF-8"); 

        Log.d("Response:", op); 
相關問題