异步任务崩溃活动

时间:2014-05-19 05:32:34

标签: android android-asynctask

我在一个活动中运行多个异步任务,而用户仍然可以切换到下一个或上一个活动,我遇到的异步任务的主要问题是当它运行并且用户启动另一个活动时它会崩溃整个应用程序导致空指针异常。 我试图在调用startActivity之前取消所有正在运行的异步任务,但仍然没有运气。

这是我的logcat

FATAL EXCEPTION: main
05-19 10:29:27.268: E/AndroidRuntime(2336): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.threeamd.timeline.activity/com.threeamd.timeline.activity.ChaptersActivity}: java.lang.NullPointerException
05-19 10:29:27.268: E/AndroidRuntime(2336):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
05-19 10:29:27.268: E/AndroidRuntime(2336):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
05-19 10:29:27.268: E/AndroidRuntime(2336):at android.app.ActivityThread.access$600(ActivityThread.java:141)
05-19 10:29:27.268: E/AndroidRuntime(2336):at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
05-19 10:29:27.268: E/AndroidRuntime(2336): at android.os.Handler.dispatchMessage(Handler.java:99)
05-19 10:29:27.268: E/AndroidRuntime(2336): at android.os.Looper.loop(Looper.java:137)
05-19 10:29:27.268: E/AndroidRuntime(2336): at android.app.ActivityThread.main(ActivityThread.java:5103)
05-19 10:29:27.268: E/AndroidRuntime(2336): at java.lang.reflect.Method.invokeNative(Native Method)
05-19 10:29:27.268: E/AndroidRuntime(2336): at java.lang.reflect.Method.invoke(Method.java:525)
05-19 10:29:27.268: E/AndroidRuntime(2336): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-19 10:29:27.268: E/AndroidRuntime(2336): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-19 10:29:27.268: E/AndroidRuntime(2336): at dalvik.system.NativeStart.main(Native Method)
05-19 10:29:27.268: E/AndroidRuntime(2336): Caused by: java.lang.NullPointerException
05-19 10:29:27.268: E/AndroidRuntime(2336): at com.threeamd.timeline.activity.ChaptersActivity.registerListners(ChaptersActivity.java:248)
05-19 10:29:27.268: E/AndroidRuntime(2336): at com.threeamd.timeline.activity.ChaptersActivity.onCreate(ChaptersActivity.java:168)
05-19 10:29:27.268: E/AndroidRuntime(2336): at android.app.Activity.performCreate(Activity.java:5133)
05-19 10:29:27.268: E/AndroidRuntime(2336): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-19 10:29:27.268: E/AndroidRuntime(2336): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
05-19 10:29:27.268: E/AndroidRuntime(2336):     ... 11 more

这是代码

private void initTweets() 
{
    lv_TweetsSection[0] = (ListView) findViewById(R.id.lv_GC_One);
    lv_TweetsSection[1] = (ListView) findViewById(R.id.lv_GC_Two);
    lv_TweetsSection[2] = (ListView) findViewById(R.id.lv_GC_Three);
    lv_TweetsSection[3] = (ListView) findViewById(R.id.lv_GC_Four);
    lv_TweetsSection[4] = (ListView) findViewById(R.id.lv_GC_Five);
    lv_TweetsSection[5] = (ListView) findViewById(R.id.lv_GC_Six);
    lv_TweetsSection[6] = (ListView) findViewById(R.id.lv_GC_Seven);

    aSyncAdapters = new ArrayList<>(7);

    for (int i = 0; i < 7; i++) {

        // new AsyncPullTweets(i+1 ,
        // this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
        GcTweetFeed adapter = new GcTweetFeed(i + 1, this);
        lv_TweetsSection[i].setAdapter(adapter);
        aSyncAdapters.add(adapter);
    }

}

在这里,我正在运行任务。我的AsyncTask位于我的适配器类

这是我的AsyncTask运行的地方

    public class GcTweetFeed extends BaseAdapter implements CustomAsyncInterface {

    public static boolean isActivityActive = false;
    private ArrayList<TweetModel> arrSectionTweets;
    private int secID;
//  private ListView refListView;
    public /*static*/ LayoutInflater inflater = null;
    public ImageLoader imageLoader;
    public View vi;
    private Activity activity;
    private CustomAsyncTask task;


    public GcTweetFeed(int secID , Activity a){
        this.secID = secID;


    }


    public void cancelRunningTask(){
        if((task.getStatus().equals(AsyncTask.Status.RUNNING))||     (task.getStatus().equals(AsyncTask.Status.PENDING))){
            task.cancel(true);
        }

    }


    public GcTweetFeed(int secID , GcTweetFeed refAdapter ){
        this.secID = secID;
    //      this.refListView = refListView;
        arrSectionTweets = new ArrayList<>();

    //      arrSectionTweets.to

    }


    @Override
    public void customonPreExecute() {
        // TODO Auto-generated method stub

    }


    @Override
    public Object customDoInBackGround(String... params) {

        Log.d("CHKD", "EXE");

            //HERE I FETCH JSON

        return null;
    }

    @Override
    public void customOnProgressUpdated() {


    }

    @Override
    public void customOnPostExecute(String result) {
        // TODO Auto-generated method stub
     //     this.refListView.setad
        if(isActivityActive){
        this.notifyDataSetChanged();
        }


        Log.d("GCTFD", ""+secID);
    //      refListView.

    }

    public int getCount() {
        if (arrSectionTweets != null) {
            return arrSectionTweets.size();
        }
        return 0;
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {


//      int xs = 100.25;
        vi = convertView;
        if (convertView == null)
            vi = inflater.inflate(R.layout.inflatetweets, null);
        TweetModel x = arrSectionTweets.get(position);

        ImageView pic = (ImageView)vi.findViewById(R.id.iv_Tw_Pic);
        imageLoader.DisplayImage(x.getProfile_picture() , R.drawable.thumbbackgroundns, pic );


         TextView tv_Name = (TextView)vi.findViewById(R.id.et_Tw_Name);
         TextView tv_TWName = (TextView)vi.findViewById(R.id.tv_Tweets_TweeterName);
         TextView tv_Tweet = (TextView)vi.findViewById(R.id.et_Tw_Tweet);

         tv_Name.setText(x.getUser_name());
         tv_TWName.setText(x.getTwitter_name());
         tv_Tweet.setText(x.getTweet());


         return vi;
        }


    }

0 个答案:

没有答案