不幸的是,<app name =“”>已停止</app>

时间:2013-12-20 09:24:18

标签: android

我知道这个问题已经有很多例子,但我似乎无法找到一个特定于我的情况的答案。我一直在关注我在网上看过的一个教程,很多反馈说它对他们有用。运行模拟器时显示带有背景的屏幕,但随后崩溃并显示错误消息“不幸的是,Sabre1已停止”。 这是我的.java文件代码

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

请帮忙。我已经被困了好几天了。感谢。

1 个答案:

答案 0 :(得分:0)

问题在于你的回答。您未在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