Zamiana kodu na Android Annotations

0

Witam, chcę przekształcić swój kod na android annotations. Apka uruchamia się ale po tapnięciu w button "aplikacja zostaje zatrzymana" a w Android Studio dostaję error. Będę wdzięczny za pomoc.:-)

FATAL EXCEPTION: main
Process: com.example.albertpula.apka3, PID: 2585
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference
at com.example.albertpula.apka3.MainActivity$ReadPic.onPreExecute(MainActivity.java:83)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:604)
at android.os.AsyncTask.execute(AsyncTask.java:551)
at com.example.albertpula.apka3.MainActivity.click(MainActivity.java:54)
at com.example.albertpula.apka3.MainActivity$1.onClick(MainActivity.java:72)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

a tu kod apki:

package com.example.albertpula.apka3;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;

import java.io.InputStream;
import java.net.URL;

@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {

 String adres ="http://www.heavens-above.com/orbitdisplay.aspx?icon=iss&width=300&height=300&satid=25544";
 Bitmap bmp;
 ProgressBar progressBar;
 Button button1;
 ImageView image;
 TextView text;

@Override
public void onCreate(Bundle savedInstanceState)
{
    image = (ImageView)findViewById(R.id.imageView);
    button1 = (Button)findViewById(R.id.button1);
    progressBar = (ProgressBar)findViewById(R.id.progressBar1);
    text = (TextView)findViewById(R.id.textView1);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}
@Click(R.id.button1)
void click(View view){
    ReadPic rp = new ReadPic();
    rp.execute();

}

class ReadPic extends AsyncTask<Void, Void, Void>
{
    boolean sukces = true;

    @Override
    protected Void doInBackground(Void... arg0){
        URL u;
        InputStream is;
        try{

            u = new URL( adres);
            is = u.openStream();
            Bitmap temp = BitmapFactory.decodeStream(is);
            bmp=temp.copy(Bitmap.Config.ARGB_8888, true);
        }
        catch( Exception e){
            sukces = false;

        }
        return null;
    }

    protected void onPreExecute(){

        progressBar.setVisibility(ProgressBar.VISIBLE);
        button1.setEnabled(false);
        super.onPreExecute();
    }
    protected void onPostExecute(Void result){
        if(sukces){
            Canvas c = new Canvas(bmp);
            Paint p = new Paint();
            int width = bmp.getWidth(), height = bmp.getHeight();
            p.setColor(Color.WHITE);
            p.setStyle(Paint.Style.STROKE);
            c.drawRect(0,0,width-1,height-1,p);

            image.setImageBitmap(bmp);
            text.setText("Downloading finished, img " +width+ "x" + height);

        }
        else{
            text.setText("Error when downloading");
        }
        button1.setEnabled(true);
        progressBar.setVisibility(ProgressBar.INVISIBLE);

        super.onPostExecute(result);
    }

}

}

0

Masz na tacy podane co jest nie tak, przeczytaj treść błędu. Pobaw się debuggerem, podglądnij w którym miejscu wywala i dlaczego i będziesz wiedział jak rozwiązać :)

1
 public void onCreate(Bundle savedInstanceState)
    {
        image = (ImageView)findViewById(R.id.imageView);
        button1 = (Button)findViewById(R.id.button1);
        progressBar = (ProgressBar)findViewById(R.id.progressBar1);
        text = (TextView)findViewById(R.id.textView1);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

Serio? Wypadało by wrócić do podstaw...

1 użytkowników online, w tym zalogowanych: 0, gości: 1, botów: 0