Tecnologia do Blogger.
RSS

Re: [androidbrasil-dev] Como fazer essa AsyncTask

Você está usando o Contexto errado para mostrar um ProgressDialog...

Você precisa de um Activity Context, e está passando o Base Context.

Lúcio Maciel
luciofm@gmail.com


2012/8/30 Diogo Henrique <diogjp10@gmail.com>:
> com o construtor
> public Async(Context context){
> this.context = context;
> }
>
>
> eu to chamando a classe assim new Async(getBaseContext()).execute();
>
> porque quando e Log nosso agente não consegue ver o erro ? kkkk
> serio estranho isso
>
> 08-30 18:47:57.387: W/dalvikvm(5868): threadid=1: thread exiting with
> uncaught exception (group=0x4001d800)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): FATAL EXCEPTION: main
> 08-30 18:47:57.399: E/AndroidRuntime(5868): java.lang.RuntimeException:
> Unable to start activity
> ComponentInfo{br.com.android.busjampa/br.com.android.busjampa.MainActivity}:
> android.view.WindowManager$BadTokenException: Unable to add window -- token
> null is not for an application
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> android.app.ActivityThread.access$2300(ActivityThread.java:125)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> android.os.Handler.dispatchMessage(Handler.java:99)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> android.os.Looper.loop(Looper.java:123)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> android.app.ActivityThread.main(ActivityThread.java:4627)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> java.lang.reflect.Method.invokeNative(Native Method)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> java.lang.reflect.Method.invoke(Method.java:521)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> dalvik.system.NativeStart.main(Native Method)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): Caused by:
> android.view.WindowManager$BadTokenException: Unable to add window -- token
> null is not for an application
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> android.view.ViewRoot.setView(ViewRoot.java:509)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> android.app.Dialog.show(Dialog.java:241)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> android.app.ProgressDialog.show(ProgressDialog.java:107)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> android.app.ProgressDialog.show(ProgressDialog.java:90)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> android.app.ProgressDialog.show(ProgressDialog.java:85)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> br.com.android.jampabus.util.Async.onPreExecute(Async.java:33)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> android.os.AsyncTask.execute(AsyncTask.java:391)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> br.com.android.busjampa.MainActivity.onCreate(MainActivity.java:73)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
> 08-30 18:47:57.399: E/AndroidRuntime(5868): ... 11 more
> 08-30 18:48:02.320: I/Process(5868): Sending signal. PID: 5868 SIG: 9
>
>
>
>
>
>
> Diogo Henrique
> (83)87344178
> Rede Sociais:
> about.me/D.H
> Meditando Provérbios:
> meditandoproverbios.blogspot.com.br
> Toastdroid: toastdroid.wordpress.com/
>
>
>
>
>
> Em 30 de agosto de 2012 15:41, Neto Marin <netomarin@gmail.com> escreveu:
>
>> Mesmo vc usando o construtor e passando o context da nullpointer?
>> Entao cola o log com o erro, assim da pra saber em qual linha está o erro.
>> E seguinte, se da um nullpointer e fala a linha, é simples vc descobrir qual
>> variável está nula no acesso...
>>
>> E NÃO mande printscreen cara... mande o log... selecione TODO o stack e
>> cole...
>>
>> ---
>> Neto Marin
>>
>> Blog: http://blog.netomarin.com
>> GTalk: netomarin@gmail.com
>> MSN: neto_marin@hotmail.com
>> Skype: netomarin
>>
>>
>> 2012/8/30 Diogo Henrique <diogjp10@gmail.com>
>>>
>>> @neto com ou sem o construtor da o mesmo erro.
>>>
>>>
>>> Diogo Henrique
>>> (83)87344178
>>> Rede Sociais:
>>> about.me/D.H
>>> Meditando Provérbios:
>>> meditandoproverbios.blogspot.com.br
>>> Toastdroid: toastdroid.wordpress.com/
>>>
>>>
>>>
>>>
>>>
>>> Em 30 de agosto de 2012 15:35, Neto Marin <netomarin@gmail.com> escreveu:
>>>
>>>> Vc cria um CONSTRUTOR q recebe context pra utiliza-lo depois, e ai
>>>> depois faz um new Async().execute() ... adivinhe onde é o NullPointer...
>>>>
>>>> []s
>>>> ---
>>>> Neto Marin
>>>>
>>>> Blog: http://blog.netomarin.com
>>>> GTalk: netomarin@gmail.com
>>>> MSN: neto_marin@hotmail.com
>>>> Skype: netomarin
>>>>
>>>>
>>>>
>>>> 2012/8/30 Diogo Henrique <diogjp10@gmail.com>
>>>>>
>>>>> Olá senhores,
>>>>>
>>>>> Eu vou precisar inserir no bando de dados SQLite uns 1000 e poucos
>>>>> dados que vem de um arquivo de texto, AsyncTask é a solução mas não estou
>>>>> conseguindo implementar ela.
>>>>> O Log diz que e NullPoint, ai acho que estou fazendo alguma coisa
>>>>> errada. Segue a classe
>>>>>
>>>>>
>>>>>
>>>>> @SuppressLint("ParserError") public class Async extends AsyncTask<Void,
>>>>> Void, Void> {
>>>>>
>>>>> private ProgressDialog progress;
>>>>> private Context context;
>>>>> private ParadaDAO paradadao;
>>>>> public Async(Context context){
>>>>> this.context = context;
>>>>> }
>>>>> @SuppressLint("ParserError") @Override
>>>>> protected void onPreExecute() {
>>>>> // mostra a o ProgressDialog
>>>>> progress = ProgressDialog.show(context, "Aguarde", "Espere alguns
>>>>> instantes");
>>>>> }
>>>>> protected Void doInBackground(Void... params) {
>>>>> paradadao = new ParadaDAO(context);
>>>>> ParadaVO vo = new ParadaVO();
>>>>> try {
>>>>> byFile(R.raw.paradas,paradadao, vo);
>>>>> } catch (IOException e) {
>>>>>
>>>>> Log.i("jampabus","erro = " + e.getMessage());
>>>>>
>>>>> e.printStackTrace();
>>>>> }
>>>>> return null;
>>>>> }
>>>>>
>>>>> protected void onPostExecute(Void result) {
>>>>> progress.dismiss();
>>>>> };
>>>>> protected void byFile(int fileID, ParadaDAO dao,ParadaVO vo) throws
>>>>> IOException {
>>>>> Log.i("jampabus","entrou no byfile");
>>>>>
>>>>> try {
>>>>> Double x = 0.0;
>>>>> Double y = 0.0;
>>>>> String coberta = "sim";
>>>>>
>>>>> BufferedReader br = new BufferedReader(new
>>>>> InputStreamReader(context.getResources().openRawResource(fileID)));
>>>>>
>>>>> String line;
>>>>>
>>>>> while ((line = br.readLine()) != null){
>>>>>
>>>>> if (line.length() > 0) {
>>>>>
>>>>>
>>>>> if(line.equals("DESCRIPTION=Unknown Point Feature")){
>>>>> continue;
>>>>> }
>>>>>
>>>>> x = Double.valueOf(line.substring(0,13));
>>>>> y = Double.valueOf(line.substring(14,28));
>>>>>
>>>>> vo.setCoberta(coberta);
>>>>> vo.setCoordenadax(x);
>>>>> vo.setCoordenaday(y);
>>>>>
>>>>> paradadao.insert(vo);
>>>>>
>>>>>
>>>>> }
>>>>>
>>>>> }
>>>>> } catch (NumberFormatException e) {
>>>>> e.printStackTrace();
>>>>> Log.i("jampa", "number = " + e.getMessage());
>>>>> } catch (NotFoundException e) {
>>>>> e.printStackTrace();
>>>>> Log.i("jampabuss","notfound = " + e.getMessage());
>>>>> }
>>>>>
>>>>>
>>>>>
>>>>> }//fim do metodo byFile()
>>>>> }
>>>>>
>>>>>
>>>>> pra chamar faço isso eu to fazendo assim new Async().execute();
>>>>> tem alguma coisa que estou fazendo de errado ?
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> Diogo Henrique
>>>>> (83)87344178
>>>>> Rede Sociais:
>>>>> about.me/D.H
>>>>> Meditando Provérbios:
>>>>> meditandoproverbios.blogspot.com.br
>>>>> Toastdroid: toastdroid.wordpress.com/
>>>>>
>>>>>
>>>>>
>>>>
>>>
>>
>

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • RSS

0 comentários:

Postar um comentário