无法启动活动组件信息空指针异常

时间:2013-10-26 20:58:23

标签: java android image android-intent nullpointerexception

在这个应用程序中,我创建了几个类别来从本地服务器带来新闻,它在类Art中我写了一个代码,允许从服务器中的URL加载图像,当我启动我的模拟器时选择Art,强行关闭

package com.example.mobilemagazine;


 import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;




import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class Art extends ListActivity {

    // Progress Dialog
    private ProgressDialog pDialog;

    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> newsList;

    // url to get all products list
    private static String url_all_news = "http://10.0.2.2/android_connect/news_art.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_NEWS = "news";
    private static final String TAG_NEWID = "id";
    private static final String TAG_TITLE = "title";




    // products JSONArray
    JSONArray News = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news);

         // Loader image - will be shown before loading image
        int loader = R.drawable.ic_launcher;

        // Imageview to show
        ImageView image = (ImageView) findViewById(R.id.image);

        // Image url
        String image_url = "http://10.0.2.2/Images/1.jpg";

        // ImageLoader class instance
        ImageLoader imgLoader = new ImageLoader(getApplicationContext());

        // whenever you want to load an image from url
        // call DisplayImage function
        // url - image url to load
        // loader - loader image, will be displayed before getting image
        // image - ImageView 
        imgLoader.DisplayImage(image_url, loader, image);





        // Hashmap for ListView
        newsList = new ArrayList<HashMap<String, String>>();

        // Loading products in Background Thread


        // Get listview
        ListView lv = getListView();

        // on seleting single product
        // launching Edit Product Screen
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting values from selected ListItem
                String nid = ((TextView) view.findViewById(R.id.newid)).getText()
                        .toString();

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        Details.class);
                // sending nid to next activity
                in.putExtra(TAG_NEWID, nid);

                // starting new activity and expecting some response back
                startActivityForResult(in, 100);
            }
        });
       new LoadAllProducts().execute();
    }

    // 
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // if result code 100
        if (resultCode == 100) {
            // if result code 100 is received 
            // means user edited/deleted product
            // reload this screen again
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        }

    }


    class LoadAllProducts extends AsyncTask<String, String, String> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(Art.this);
            pDialog.setMessage("Loading News. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }


        protected String doInBackground(String... args) {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_all_news, "GET", params);

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

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {
                    // products found
                    // Getting Array of Products
                    News = json.getJSONArray(TAG_NEWS);

                    // looping through All Products
                    for (int i = 0; i < News.length(); i++) {
                        JSONObject c = News.getJSONObject(i);

                        // Storing each json item in variable
                        String id = c.getString(TAG_NEWID);
                        String title = c.getString(TAG_TITLE);



                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        map.put(TAG_NEWID, id);
                        map.put(TAG_TITLE, title);


                        // adding HashList to ArrayList
                        newsList.add(map);
                    }
                }
                 else {
                    // no products found
                    // Launch Add New product Activity
                    Intent i = new Intent(getApplicationContext(), Empty.class);
                    // Closing all previous activities
                    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                }   



            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }


        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() {

                    ListAdapter adapter = new SimpleAdapter(
                            Art.this, newsList,
                            R.layout.tab_new, new String[] { TAG_NEWID, TAG_TITLE}, 

                                            new int[] { R.id.newid, R.id.newtitle });

                    // updating listview
                    setListAdapter(adapter);
                }
            });

        }}

}   

LOG CAT:

10-26 20:30:37.738: E/AndroidRuntime(311): FATAL EXCEPTION: main
10-26 20:30:37.738: E/AndroidRuntime(311): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mobilemagazine/com.example.mobilemagazine.TabList}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mobilemagazine/com.example.mobilemagazine.Art}: java.lang.NullPointerException
10-26 20:30:37.738: E/AndroidRuntime(311):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-26 20:30:37.738: E/AndroidRuntime(311):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-26 20:30:37.738: E/AndroidRuntime(311):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-26 20:30:37.738: E/AndroidRuntime(311):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-26 20:30:37.738: E/AndroidRuntime(311):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 20:30:37.738: E/AndroidRuntime(311):  at android.os.Looper.loop(Looper.java:123)
10-26 20:30:37.738: E/AndroidRuntime(311):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-26 20:30:37.738: E/AndroidRuntime(311):  at java.lang.reflect.Method.invokeNative(Native Method)
10-26 20:30:37.738: E/AndroidRuntime(311):  at java.lang.reflect.Method.invoke(Method.java:521)
10-26 20:30:37.738: E/AndroidRuntime(311):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-26 20:30:37.738: E/AndroidRuntime(311):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-26 20:30:37.738: E/AndroidRuntime(311):  at dalvik.system.NativeStart.main(Native Method)
10-26 20:30:37.738: E/AndroidRuntime(311): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mobilemagazine/com.example.mobilemagazine.Art}: java.lang.NullPointerException
10-26 20:30:37.738: E/AndroidRuntime(311):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-26 20:30:37.738: E/AndroidRuntime(311):  at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
10-26 20:30:37.738: E/AndroidRuntime(311):  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
10-26 20:30:37.738: E/AndroidRuntime(311):  at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
10-26 20:30:37.738: E/AndroidRuntime(311):  at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
10-26 20:30:37.738: E/AndroidRuntime(311):  at android.widget.TabHost.setCurrentTab(TabHost.java:323)
10-26 20:30:37.738: E/AndroidRuntime(311):  at android.widget.TabHost.addTab(TabHost.java:213)
10-26 20:30:37.738: E/AndroidRuntime(311):  at com.example.mobilemagazine.TabList.onCreate(TabList.java:68)
10-26 20:30:37.738: E/AndroidRuntime(311):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-26 20:30:37.738: E/AndroidRuntime(311):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-26 20:30:37.738: E/AndroidRuntime(311):  ... 11 more
10-26 20:30:37.738: E/AndroidRuntime(311): Caused by: java.lang.NullPointerException
10-26 20:30:37.738: E/AndroidRuntime(311):  at com.example.mobilemagazine.ImageLoader.DisplayImage(ImageLoader.java:46)
10-26 20:30:37.738: E/AndroidRuntime(311):  at com.example.mobilemagazine.Art.onCreate(Art.java:78)
10-26 20:30:37.738: E/AndroidRuntime(311):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-26 20:30:37.738: E/AndroidRuntime(311):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-26 20:30:37.738: E/AndroidRuntime(311):  ... 20 more

0 个答案:

没有答案