2013-12-20 90 views
-1

我知道這個問題已經有很多例子了,但我似乎無法找到針對我的具體情況的答案。我一直在關注我在網上看到的一個教程,許多反饋都說它對他們有效。運行模擬器後,顯示屏幕顯示背景,但隨後崩潰並顯示錯誤消息「不幸的是,Sabre1已停止。」 這裏是我的java文件的代碼不幸的是,<App Name>已停止

public class MainActivity extends Activity { 
    private String jsonResult; 
    private String url = "localhost/foldername/connection php"; //-my connection string in .php 
    private ListView listView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     listView = (ListView) findViewById(R.id.listView1); 
     accessWebService(); 
    } 

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

    // Async Task to access the web 
    private class JsonReadTask extends AsyncTask<String, Void, String> { 
     @Override 
     protected String doInBackground(String... params) { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(params[0]); 
     try { 
     HttpResponse response = httpclient.execute(httppost); 
     jsonResult = inputStreamToString(
      response.getEntity().getContent()).toString(); 
     } 

     catch (ClientProtocolException e) { 
     e.printStackTrace(); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     } 
     return null; 
     } 

    private StringBuilder inputStreamToString(InputStream is) { 
      String rLine = ""; 
      StringBuilder answer = new StringBuilder(); 
      BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 

      try { 
      while ((rLine = rd.readLine()) != null) { 
      answer.append(rLine); 
      } 
      } 

      catch (IOException e) { 
      // e.printStackTrace(); 
      Toast.makeText(getApplicationContext(), 
       "Error..." + e.toString(), Toast.LENGTH_LONG).show(); 
      } 
      return answer; 
      } 

    @Override 
     protected void onPostExecute(String result) { 
     ListDrwaer(); 
     } 
    }// end async task 

    public void accessWebService() { 
     JsonReadTask task = new JsonReadTask(); 
     // passes values for the urls string array 
     task.execute(new String[] { url }); 
    } 

    // build hash set for list view 
    public void ListDrwaer() { 
     List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>(); 

     try { 
     JSONObject jsonResponse = new JSONObject(jsonResult); 
     JSONArray jsonMainNode = jsonResponse.optJSONArray("login_tb"); 

     for (int i = 0; i < jsonMainNode.length(); i++) { 
     JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
     String Fname = jsonChildNode.optString("Fname"); 
     String Mname = jsonChildNode.optString("Mname"); 
     String Lname = jsonChildNode.optString("Lname"); 
     String number = jsonChildNode.optString("Employee_ID"); 
     String outPut = Fname + "-" + Mname + "-" + Lname + "-" + number; 
     employeeList.add(createEmployee("employees", outPut)); 
     } 
     } catch (JSONException e) { 
     Toast.makeText(getApplicationContext(), "Error" + e.toString(), 
     Toast.LENGTH_SHORT).show(); 
     } 

     SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList, 
     android.R.layout.simple_list_item_1, 
     new String[] { "employees" }, new int[] { android.R.id.text1 }); 
     listView.setAdapter(simpleAdapter); 
     } 

    private HashMap<String, String> createEmployee(String name, String number) { 
     HashMap<String, String> employeeNameNo = new HashMap<String, String>(); 
     employeeNameNo.put(name, number); 
     return employeeNameNo; 
    } 

} 

這裏是我的.xml文件的代碼

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" 
    android:background="@drawable/adservewelcome"> 

    <ListView 
     android:id="@+id/listView1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="14dp" > 
    </ListView> 

</RelativeLayout> 

這裏是我的logcat錯誤

12-20 04:08:04.360: E/memtrack(2466): Couldn't load memtrack module (No such file or directory) 
12-20 04:08:04.360: E/android.os.Debug(2466): failed to load memtrack module: -2 
12-20 04:13:13.410: E/SoundPool(1820): error loading /system/media/audio/ui/Effect_Tick.ogg 
12-20 04:13:13.420: E/SoundPool(1820): error loading /system/media/audio/ui/Effect_Tick.ogg 
12-20 04:13:13.430: E/SoundPool(1820): error loading /system/media/audio/ui/Effect_Tick.ogg 
12-20 04:13:13.430: E/SoundPool(1820): error loading /system/media/audio/ui/Effect_Tick.ogg 
12-20 04:13:13.430: E/SoundPool(1820): error loading /system/media/audio/ui/Effect_Tick.ogg 
12-20 04:13:13.430: E/SoundPool(1820): error loading /system/media/audio/ui/KeypressStandard.ogg 
12-20 04:13:13.430: E/SoundPool(1820): error loading /system/media/audio/ui/KeypressSpacebar.ogg 
12-20 04:13:13.500: E/SoundPool(1820): error loading /system/media/audio/ui/KeypressDelete.ogg 
12-20 04:13:13.500: E/SoundPool(1820): error loading /system/media/audio/ui/KeypressReturn.ogg 
12-20 04:13:13.600: E/SoundPool(1820): error loading /system/media/audio/ui/KeypressInvalid.ogg 
12-20 04:13:21.960: E/SoundPool(1820): error loading /system/media/audio/ui/Effect_Tick.ogg 
12-20 04:13:22.020: E/SoundPool(1820): error loading /system/media/audio/ui/Effect_Tick.ogg 
12-20 04:13:22.140: E/SoundPool(1820): error loading /system/media/audio/ui/Effect_Tick.ogg 
12-20 04:13:22.160: E/SoundPool(1820): error loading /system/media/audio/ui/Effect_Tick.ogg 
12-20 04:13:22.170: E/SoundPool(1820): error loading /system/media/audio/ui/Effect_Tick.ogg 
12-20 04:13:22.240: E/SoundPool(1820): error loading /system/media/audio/ui/KeypressStandard.ogg 
12-20 04:13:22.240: E/SoundPool(1820): error loading /system/media/audio/ui/KeypressSpacebar.ogg 
12-20 04:13:22.250: E/SoundPool(1820): error loading /system/media/audio/ui/KeypressDelete.ogg 
12-20 04:13:22.270: E/SoundPool(1820): error loading /system/media/audio/ui/KeypressReturn.ogg 
12-20 04:13:22.280: E/SoundPool(1820): error loading /system/media/audio/ui/KeypressInvalid.ogg 
12-20 04:13:22.940: E/libEGL(49): called unimplemented OpenGL ES API 
12-20 04:13:22.940: E/libEGL(49): called unimplemented OpenGL ES API 
12-20 04:13:22.940: E/libEGL(49): called unimplemented OpenGL ES API 
12-20 04:13:22.950: E/libEGL(49): called unimplemented OpenGL ES API 
12-20 04:13:22.950: E/SurfaceFlinger(49): glCheckFramebufferStatusOES error -1603666533 
12-20 04:13:22.950: E/SurfaceFlinger(49): got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot 
12-20 04:13:22.950: E/libEGL(49): called unimplemented OpenGL ES API 
12-20 04:13:22.950: E/libEGL(49): called unimplemented OpenGL ES API 
12-20 04:13:38.110: E/AndroidRuntime(2540): FATAL EXCEPTION: main 
12-20 04:13:38.110: E/AndroidRuntime(2540): Process: com.example.sabre1, PID: 2540 
12-20 04:13:38.110: E/AndroidRuntime(2540): java.lang.NullPointerException 
12-20 04:13:38.110: E/AndroidRuntime(2540):  at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116) 
12-20 04:13:38.110: E/AndroidRuntime(2540):  at org.json.JSONTokener.nextValue(JSONTokener.java:94) 
12-20 04:13:38.110: E/AndroidRuntime(2540):  at org.json.JSONObject.<init>(JSONObject.java:155) 
12-20 04:13:38.110: E/AndroidRuntime(2540):  at org.json.JSONObject.<init>(JSONObject.java:172) 
12-20 04:13:38.110: E/AndroidRuntime(2540):  at com.example.sabre1.MainActivity.ListDrwaer(MainActivity.java:105) 
12-20 04:13:38.110: E/AndroidRuntime(2540):  at com.example.sabre1.MainActivity$JsonReadTask.onPostExecute(MainActivity.java:90) 
12-20 04:13:38.110: E/AndroidRuntime(2540):  at com.example.sabre1.MainActivity$JsonReadTask.onPostExecute(MainActivity.java:1) 
12-20 04:13:38.110: E/AndroidRuntime(2540):  at android.os.AsyncTask.finish(AsyncTask.java:632) 
12-20 04:13:38.110: E/AndroidRuntime(2540):  at android.os.AsyncTask.access$600(AsyncTask.java:177) 
12-20 04:13:38.110: E/AndroidRuntime(2540):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) 
12-20 04:13:38.110: E/AndroidRuntime(2540):  at android.os.Handler.dispatchMessage(Handler.java:102) 
12-20 04:13:38.110: E/AndroidRuntime(2540):  at android.os.Looper.loop(Looper.java:137) 
12-20 04:13:38.110: E/AndroidRuntime(2540):  at android.app.ActivityThread.main(ActivityThread.java:4998) 
12-20 04:13:38.110: E/AndroidRuntime(2540):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-20 04:13:38.110: E/AndroidRuntime(2540):  at java.lang.reflect.Method.invoke(Method.java:515) 
12-20 04:13:38.110: E/AndroidRuntime(2540):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
12-20 04:13:38.110: E/AndroidRuntime(2540):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
12-20 04:13:38.110: E/AndroidRuntime(2540):  at dalvik.system.NativeStart.main(Native Method) 
12-20 04:13:38.350: E/libEGL(49): called unimplemented OpenGL ES API 
12-20 04:13:38.360: E/libEGL(49): called unimplemented OpenGL ES API 
12-20 04:13:38.360: E/libEGL(49): called unimplemented OpenGL ES API 
12-20 04:13:38.360: E/libEGL(49): called unimplemented OpenGL ES API 
12-20 04:13:38.360: E/SurfaceFlinger(49): glCheckFramebufferStatusOES error -1603666533 
12-20 04:13:38.360: E/SurfaceFlinger(49): got GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot 
12-20 04:13:38.370: E/libEGL(49): called unimplemented OpenGL ES API 
12-20 04:13:38.370: E/libEGL(49): called unimplemented OpenGL ES API 

請幫助。我被困了好幾天。謝謝。

+0

什麼是在該行的com.example.sabre1.MainActivity.ListDrwaer(MainActivity.java:105)您MainActivity.java的 –

+0

檢查行號105。 –

+0

你的'105號線'是什麼? – GrIsHu

回答

1

問題在於您的迴應。您沒有從doInBackground獲得服務器的響應。

這個方法(onPostExecute)在調用方法之前檢查這個驗證。

@Override 
protected void onPostExecute(String result) { 
    if (jsonResult != null && jsonResult.equalsIgnoreCase("")) { 
    ListDrwaer();    
    } 
} 

更新的代碼:

doInBackground,因爲它是默認返回值類型是字符串,那麼你應該做的是這樣,而不是宣佈參加JSON_RESPONSE全局變量。

// Async Task to access the web 
private class JsonReadTask extends AsyncTask<String, Void, String> { 

    @Override 
    protected String doInBackground(String... params) { 
     String mResponse = ""; 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(params[0]); 
     try { 
      HttpResponse response = httpclient.execute(httppost); 
      mResponse = inputStreamToString(response.getEntity().getContent()).toString(); 
     } catch (ClientProtocolException e) { 
      mResponse = ""; 
      e.printStackTrace(); 
     } catch (IOException e) { 
      mResponse = ""; 
      e.printStackTrace(); 
     } 
     return mResponse; 
    } 

    private StringBuilder inputStreamToString(InputStream is) { 
     String rLine = ""; 
     StringBuilder answer = new StringBuilder(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(is)); 
     try { 
      while ((rLine = rd.readLine()) != null) { 
       answer.append(rLine); 
      } 
     } catch (IOException e) { 
      // Yon must not refresh views from the doInBackground method. 
      // Toast.makeText(getApplicationContext(), "Error..." + e.toString(), Toast.LENGTH_LONG).show(); 
     } 
     return answer; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     if (result != null && result.equalsIgnoreCase("")) { 
      ListDrwaer(result); 
     } 
    } 
}// end async task 
+0

我試過以上報價,但同樣的問題。 –

+0

@ Saint-Clair調試以及您是否從服務器獲取任何內容。並展示你得到的任何東西。 – SilentKiller

相關問題