Search code examples
javaandroidandroid-annotations

Change code to android annotations


I'm trying to change my code to android annotations. My app starts but when I tap button it crash and get error:

FATAL EXCEPTION: main
Process: com.example.albertpula.apka3, PID: 2523
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)

here is code:

@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);
    }
}
}

Solution

    1. Use ViewBinding for binding your xml views like this :

      @ViewById(R.id.button1)Button button1;
      
      @ViewById(R.id.imageView)ImageView image;
      
      @ViewById(R.id.textView1)TextView text;
      

    Remove the code from your onCreate().

    1. Don't add :

      super.onCreate(savedInstanceState);
      
      setContentView(R.layout.activity_main);
      
    2. For the error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference at

    Option :1 Check if it is null.

    protected void onPreExecute(){
        if(progressBar != null)
            progressBar.setVisibility(ProgressBar.VISIBLE);
    
        button1.setEnabled(false);
        super.onPreExecute();
    }
    

    AND,

    button1.setEnabled(true); 
    
    if(progressBar != null)
    progressBar.setVisibility(ProgressBar.INVISIBLE);
    
    super.onPostExecute(result);
    

    Option :2 - Initialize the ProgressDialog

     class ReadPic extends AsyncTask<Void, Void, Void> {
         ProgressBar progressBar;
         boolean sukces = true;
    
         public ReadPic(){
             progressBar = new ProgressBar();//Set the Theme, context & call constructor as per your choice.
         }
    
         //YOUR CODE GOES BELOW// 
      }//ReadPic class closes here....
    

    Edit 1: Gorthez, instantiate your Progress bar like this:

    progressBar = (ProgressBar) findViewById(R.id.progress_bar);

    Also Add Progress Bar in your xml:

    <LinearLayout
          android:orientation="horizontal"
          ... >
            <ProgressBar
              android:id="@+id/progress_bar"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              style="@android:style/Widget.ProgressBar.Small"
              android:layout_marginRight="5dp" />
          <TextView
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/loading" />  </LinearLayout>
    

    Edit1 Reference https://developer.android.com/reference/android/widget/ProgressBar.html