Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/208.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java setImageResource在调用时使应用程序崩溃_Java_Android - Fatal编程技术网

Java setImageResource在调用时使应用程序崩溃

Java setImageResource在调用时使应用程序崩溃,java,android,Java,Android,我尝试设置IV_status.setImageResource(R.drawable.red) 但它似乎不起作用,每当调用上述代码时,整个应用程序都会运行 将崩溃。我还尝试了setBackgroundResource(R.drawable.red),但同样的问题仍然存在 正在发生。它在进入活动屏幕前崩溃 public class PhotosActivity extends ListActivity { // Progress Dialog private ProgressDialog

我尝试设置
IV_status.setImageResource(R.drawable.red)

但它似乎不起作用,每当调用上述代码时,整个应用程序都会运行

将崩溃。我还尝试了
setBackgroundResource(R.drawable.red)
,但同样的问题仍然存在

正在发生。它在进入活动屏幕前崩溃

public class PhotosActivity extends ListActivity {
// Progress Dialog
    private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();
    ArrayList<HashMap<String, String>> productsList;
    // url to get all products list
    private static String url_all_products = "http://10.0.8.41/pos/get_all_products.php";

    // JSON Node names

    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PRODUCTS = "tables";
    private static final String TAG_PID = "tableID";
    private static final String TAG_NAME = "tableName";
    private static final String TAG_AREA = "area";
    private static final String TAG_STATUS = "status";
    static ImageView IV_status;
    // products JSONArray
    JSONArray products = null;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.photos_layout);
    IV_status = (ImageView) findViewById(R.id.status);

    IV_status.setImageResource(R.drawable.red);
    // Hashmap for ListView
            productsList = new ArrayList<HashMap<String, String>>();

            // Loading products in Background Thread
            new LoadAllProducts().execute();

            // Get listview
            ListView lv = getListView();

            // on seleting single product
            // launching Edit Product Screen



        }

        // Response from Edit Product Activity
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            // if result code 100
            if (resultCode == 100) {
                Intent intent = getIntent();
                finish();
                startActivity(intent);
            }

        }

        /**
         * Background Async Task to Load all product by making HTTP Request
         * */
        class LoadAllProducts extends AsyncTask<String, String, String> {

            /**
             * Before starting background thread Show Progress Dialog
             * */
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(PhotosActivity.this);
                pDialog.setMessage("Loading products. Please wait...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(false);
                pDialog.show();
            }

            /**
             * getting All products from url
             * */
            protected String doInBackground(String... args) {

                List<NameValuePair> params = new ArrayList<NameValuePair>();
                // getting JSON string from URL
                JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

                // Check your log cat for JSON reponse
                Log.d("All Products: ", json.toString());

                try {
                    int success = json.getInt(TAG_SUCCESS);

                    if (success == 1) {

                        products = json.getJSONArray(TAG_PRODUCTS);


                        for (int i = 0; i < products.length(); i++) {
                            JSONObject c = products.getJSONObject(i);

                            // Storing each json item in variable
                            String id = c.getString(TAG_PID);
                            String name = c.getString(TAG_NAME);
                            String area = c.getString(TAG_AREA);
                            String status = c.getString(TAG_STATUS);
                            // creating new HashMap
                            HashMap<String, String> map = new HashMap<String, String>();

                            // adding each child node to HashMap key => value
                            map.put(TAG_PID, id);
                            map.put(TAG_NAME, name);
                            map.put(TAG_AREA,area);
                            map.put(TAG_STATUS, status);
                            // adding HashList to ArrayList
                            if(area.equals("A")){

                                    productsList.add(map);


                            }



                    } else {
                        // no products found
                        // Launch Add New product Activity
                        Intent i = new Intent(getApplicationContext(),PhotosActivity.class);// Closing all previous activities
                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(i);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                return null;
            }

            /**
             * After completing background task Dismiss the progress dialog
             * **/
            protected void onPostExecute(String file_url) {
                // dismiss the dialog after getting all products
                pDialog.dismiss();
                // updating UI from Background Thread
                runOnUiThread(new Runnable() {
                    public void run() {
                        /**
                         * Updating parsed JSON data into ListView
                         * */

                        ListAdapter adapter = new SimpleAdapter(
                                PhotosActivity.this, productsList,R.layout.list_item, new String[] { TAG_PID,TAG_NAME},
                                new int[] { R.id.pid, R.id.name });
                        // updating listview
                        setListAdapter(adapter);
                    }
                });

            }

        }
    }
列表视图

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">


    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

您需要像这样修改您的行

IV_status.setImageResource(R.drawable.red)
对此

IV_status.setImageDrawable(getResources().getDrawable(R.drawable.red));
现在运行你的代码,它会工作的


应用程序崩溃的原因是它没有获取资源,因此您需要如上所述为它提供资源跟踪

请发布错误的堆栈跟踪,您可以从日志中获取它。从日志中提取stacktrace将是有用的。IV_status=(ImageView)findViewById(R.id.status);请检查Imageview的id,该id与Logcat所需的xml layoutStack跟踪中的id相同。。。。。!!!其中是指向类型ImageView中的方法setImageResource(int)不适用于参数(Drawable)的错误。我从API引用中看到,setImageResource(int)从xml布局接受资源id,而不是Drawable对象。尽管使用了上述IV_status.setImageDrawable,您也可以使用以下方法(getResources().getDrawable(R.drawable.red));仍在崩溃..在添加IV_status.setImageDrawable(getResources().getDrawable(R.drawable.red));@user2408603一个可能的错误是您的xml中没有id
android:id=“@android:id/list”
IV_status.setImageResource(R.drawable.red)
IV_status.setImageDrawable(getResources().getDrawable(R.drawable.red));