2014-12-05 73 views
2

我一直在嘗試實現片段佈局,並在其中一個片段類中使用asynctask。 到目前爲止,我無法從片段的oncreateView調用asynctask方法。 當我運行的應用程序及其給錯誤「不幸的是,應用程序已經停止」從片段的onCreateView調用AsyncTask方法,失敗

這裏是我的片段代碼

package com.example.mohamed.myapplication; 

import java.io.File; 
import java.io.IOException; 

import org.jsoup.Connection; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Attributes; 
import org.jsoup.nodes.Document; 
import org.jsoup.nodes.Element; 
import org.jsoup.select.Elements; 

import android.app.ActionBar; 
import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v13.app.FragmentPagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 
import android.widget.Toast; 
import android.widget.ListView; 


import java.util.Locale; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 


public class FirstFragment extends Fragment { 

    // URL Address 
    String url = "http://www.androidbegin.com"; 
    ProgressDialog mProgressDialog; 
    AsyncTask aTask; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 

     new Title().execute(); 

     View rootView = inflater.inflate(R.layout.first_frag, container, false); 
     return rootView; 

    } 


    // Title AsyncTask 
    private class Title extends AsyncTask<Void, Void, Void> { 
     String title; 

//  Context mcontext; 
//  public Title(Context c){ 
//   mcontext=c; 
//  } 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
//    mProgressDialog = new ProgressDialog(mcontext); 
//    mProgressDialog.setTitle("Android Basic JSoup Tutorial"); 
//    mProgressDialog.setMessage("Loading..."); 
//    mProgressDialog.setIndeterminate(false); 
//    mProgressDialog.show(); 
     } 

     @Override 
     protected Void doInBackground(Void... params) { 
      try { 
       // Connect to the web site 
       Document document = Jsoup.connect(url).get(); 
       // Get the html document title 
       title = document.title(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     @Override 
     protected void onPostExecute(Void result) { 
      // Set title into TextView 
      TextView txttitle = (TextView) getView().findViewById(R.id.First); 
      txttitle.setText(title); 
      mProgressDialog.dismiss(); 
     } 
    } 

    public static FirstFragment newInstance(String text) { 

     FirstFragment f = new FirstFragment(); 
     Bundle b = new Bundle(); 
     b.putString("msg", text); 

     f.setArguments(b); 

     return f; 

    } 

} 

那麼,什麼是調用的AsyncTask的正確方法?

,這裏是我的logcat

12-01 14:44:40.052 1093-1093/com.example.mohamed.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed 49K, 3% free 3755K/3868K, paused 2ms, total 4ms 
12-01 14:44:40.056 1093-1093/com.example.mohamed.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed 3K, 3% free 3973K/4092K, paused 2ms, total 2ms 
12-01 14:44:40.064 1093-1093/com.example.mohamed.myapplication I/dalvikvm-heap﹕ Grow heap (frag case) to 6.356MB for 2536932-byte allocation 
12-01 14:44:40.072 1093-1103/com.example.mohamed.myapplication D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 2% free 6450K/6572K, paused 5ms, total 5ms 
12-01 14:44:40.180 1093-1093/com.example.mohamed.myapplication D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so 
12-01 14:44:40.180 1093-1093/com.example.mohamed.myapplication D/﹕ HostConnection::get() New Host Connection established 0xb8a8f1c0, tid 1093 
12-01 14:44:40.184 1093-1093/com.example.mohamed.myapplication D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so 
12-01 14:44:40.184 1093-1093/com.example.mohamed.myapplication D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so 
12-01 14:44:40.224 1093-1093/com.example.mohamed.myapplication W/EGL_genymotion﹕ eglSurfaceAttrib not implemented 
12-01 14:44:40.224 1093-1093/com.example.mohamed.myapplication E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache 
12-01 14:44:40.228 1093-1093/com.example.mohamed.myapplication E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 8192 
12-01 14:44:40.232 1093-1093/com.example.mohamed.myapplication E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints() 
12-01 14:44:40.232 1093-1093/com.example.mohamed.myapplication E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 8192 
12-01 14:44:40.236 1093-1093/com.example.mohamed.myapplication D/OpenGLRenderer﹕ Enabling debug mode 0 
+0

你的代碼在哪裏調用'AsyncTask'?並請張貼'logcat' – 2014-12-05 13:53:53

+0

@BlazeTama新標題().exec()在onCreateView – Suvitruf 2014-12-05 13:55:14

+0

ups對不起,我的眼睛滑落 – 2014-12-05 13:57:01

回答

2

您試圖創建視圖之前在onCreateView使用AsyncTask

試圖調用的AsyncTask在onActivityCreated

@Override 
public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    new Title().execute(); 
} 
+0

問題在於這一行。 'mProgressDialog.dismiss();'我剛剛評論過這一行,並從onActivityCreated調用了AsyncTask。最後問題解決了。謝謝大家的幫助 – 2014-12-06 05:28:28

2

我認爲問題就出在這行:

TextView txttitle = (TextView) getView().findViewById(R.id.First); 

刪除線,並在onCreateView加入這一行:

txttitle = (TextView) rootview.findViewById(R.id.First); 

使txttitle成爲一個全局變量。

後,在onActivityCreated

1

你在你的代碼中的競爭條件執行AsyncTask。您從onCreateView()方法開始Title,方法是在返回rootView之前。因此,如果AsyncTask在之前執行並完成了Fragment已成功運行到運行狀態,那麼您的onPostExecute()中將不會從getView()中收到任何內容。此外,代碼不會顯示何時啓動對話框,因此您可以在嘗試訪問mProgressDialog時獲得NullPointerException

順便說一下,你的logat是不完整的:它缺少實際的崩潰堆棧跟蹤。找到它,它會告訴你到底發生了什麼異常。