在targetSdkVersion 8上运行AsyncTask时出现Android崩溃

时间:2012-08-18 18:26:26

标签: android android-asynctask nullpointerexception imageview

当我设置android:targetSdkVersion="8"时我的应用程序崩溃但是当我将其设置为android:targetSdkVersion="11"时,一切都很顺利。请看logcat,我真的不知道我的代码有什么问题。看起来任何使用11以下SDK的设备都会遇到此问题。我的最小sdk是7.如果我继续提供我的应用程序,我可能遇到很多麻烦。

public class SingleMenuItemActivity  extends Activity {

    // XML node keys
static final String KEY_TITLE = "title";
static final String KEY_ARTIST = "artist";  
static final String KEY_THUMB_URL = "thumb_url";
private ProgressDialog pDialog;
String title;
String artist;
String image_url;
ImageView view;
Intent intent;
Context context;
ShareActionProvider mShareActionProvider;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.single_list_item);
        new loadSingleView().execute(); 




        view = (ImageView) findViewById(R.id.single_image);

    }

public class loadSingleView extends AsyncTask<String, String, String> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(
                    SingleMenuItemActivity.this);
            pDialog.setMessage("Connecting to Server ...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }
        @Override
        protected String doInBackground(String... args) {
            // updating UI from Background Thread


            Intent in = getIntent();

            image_url = in.getStringExtra(KEY_THUMB_URL);
                        title = in.getStringExtra(KEY_TITLE);
            artist = in.getStringExtra(KEY_ARTIST);

            return null;

                    }
        @Override       
        protected void onPostExecute(String args) {
            // dismiss the dialog after getting all products

            ImageLoader imgLoader = new ImageLoader(getApplicationContext());

            imgLoader.DisplayImage(image_url, view);

            TextView lblName = (TextView) findViewById(R.id.name_title);
            TextView lblCost = (TextView) findViewById(R.id.name_artist);



            lblName.setText(title);
            lblCost.setText(artist);

            ActionBar ab = getActionBar();
            ab.setTitle(title);

            pDialog.dismiss();

        }


}   

logcat的:

08-19 02:19:03.351: E/AndroidRuntime(22761): FATAL EXCEPTION: main
08-19 02:19:03.351: E/AndroidRuntime(22761): java.lang.NullPointerException
08-19 02:19:03.351: E/AndroidRuntime(22761):    at com.example.androidhive.SingleMenuItemActivity$loadSingleView.onPostExecute(SingleMenuItemActivity.java:88)
08-19 02:19:03.351: E/AndroidRuntime(22761):    at com.example.androidhive.SingleMenuItemActivity$loadSingleView.onPostExecute(SingleMenuItemActivity.java:1)
08-19 02:19:03.351: E/AndroidRuntime(22761):    at android.os.AsyncTask.finish(AsyncTask.java:602)
08-19 02:19:03.351: E/AndroidRuntime(22761):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
08-19 02:19:03.351: E/AndroidRuntime(22761):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
08-19 02:19:03.351: E/AndroidRuntime(22761):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 02:19:03.351: E/AndroidRuntime(22761):    at android.os.Looper.loop(Looper.java:137)
08-19 02:19:03.351: E/AndroidRuntime(22761):    at android.app.ActivityThread.main(ActivityThread.java:4512)
08-19 02:19:03.351: E/AndroidRuntime(22761):    at java.lang.reflect.Method.invokeNative(Native Method)
08-19 02:19:03.351: E/AndroidRuntime(22761):    at java.lang.reflect.Method.invoke(Method.java:511)
08-19 02:19:03.351: E/AndroidRuntime(22761):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:982)
08-19 02:19:03.351: E/AndroidRuntime(22761):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
08-19 02:19:03.351: E/AndroidRuntime(22761):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:3)

SDK 8没有ActionBar。您必须使用类似ActionBarSherlock库的内容才能获得ActionBar支持SDK&lt;蜂窝状。

答案 1 :(得分:2)

我认为这些代码行会在 API 8

中导致异常
 ActionBar ab = getActionBar();
 ab.setTitle(title);

因为, Android 3.0(API级别11)中添加了ActionBar。来自Android Docs ..

对于您的主题问题..

请看这两个SO问题..

  1. ActionBar pre Honeycomb

  2. How to implement the action bar in api levels less than 11?

答案 2 :(得分:1)

不知道第88行是哪一个,问题可能就在这里:

ab.setTitle(title);

在API 11中添加了ActionBar

http://developer.android.com/guide/topics/ui/actionbar.html