2016-09-29 47 views
0

我试图将我的代码更改为android注释。我的应用程序开始,但是当我点击按钮,它崩溃,出现错误:将代码更改为android注释

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) 

这里是代码:

@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

logcat的日志中,方便阅读单行排列并格式化 – Nikhil

回答

0
  1. 使用ViewBinding绑定您的XML看法是这样的:

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

从您的onCreate删除()的代码。

  • 不要添加:

    super.onCreate(savedInstanceState); 
    
    setContentView(R.layout.activity_main); 
    
  • 对于错误:显示java.lang.NullPointerException:尝试调用虚拟方法“无效android.widget.ProgressBar。 setVisibility(INT)”上的空对象引用在

  • 选项:1个检查它是否为空。

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

    选项:2 - 初始化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.... 
    

    编辑1:Gorthez,实例化进度条是这样的:

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

    还添加进度栏在你的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参考https://developer.android.com/reference/android/widget/ProgressBar.html

    +0

    感谢您的答复。如果我删除super.onCreate ...和setContentView ...应用程序崩溃。顺便说一句,我可以键入什么设置主题,上下文和调用构造函数(选项2)? – Gorthez

    +0

    @Gorthez:@EActivity(R.layout.activity_main)在我的代码中工作得很好。即我的代码不会崩溃。可能是你的gradle同步问题(可能)。不过!我已经更新了(选项2)的代码,现在chk。 – Pawan

    +0

    非常感谢!终于工作了! :)我有最后一个问题。此代码在tapp后仅显示一张图片,在1 tapp按钮后显示更多图片还有很多事情要做吗? – Gorthez

    0
    @Override 
    
    public void onCreate(Bundle savedInstanceState) 
    { 
    
        **//put setContentView and super call here 
    
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main);** 
    
        image = (ImageView)findViewById(R.id.imageView); 
        button1 = (Button)findViewById(R.id.button1); 
        progressBar = (ProgressBar)findViewById(R.id.progressBar1); 
        text = (TextView)findViewById(R.id.textView1); 
    
        //setContentView(R.layout.activity_main); 
    } 
    

    在这里,你试图膨胀的布局之前访问视图元素。 首先通过调用setContentView(R.layout.activity_main)来扩充布局; 然后尝试访问元素。

    +0

    谢谢你,它的帮助下,应用程序没有崩溃,而且当我敲击什么也没发生按钮。任何想法还有什么不对? 这是形式logcat 17985-17985/com.example.albertpula.apka3 I /时间线:时间线:Activity_idle id:[email protected]时间:1459738 17985-17985/com.example.albertpula.apka3 D/ViewRootImpl:ViewPostImeInputStage processPointer 0 17985-17985/com.example.albertpula.apka3 D/ViewRootImpl:ViewPostImeInputStage processPointer 1 17985-18239/com.example.albertpula.apka3 I/System.out:(HTTPLog)-Static:isSBSettingEnabled false – Gorthez

    +0

    保护无效onPreExecute(){ \t progressBar.setVisibility(ProgressBar。可见); \t // add below line \t progressBar.show(); \t \t button1.setEnabled(false); \t super.onPreExecute(); } 您应该调用progressBar.show()以使其可见 – Raman

    +0

    ups style for progressBar where missing。 我无法添加progressBar.show()“无法解析方法show()” 但仍然没有任何反应 – Gorthez