将代码更改为 android 注释
Change code to android annotations
我正在尝试将我的代码更改为 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_.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);
}
}
}
@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); 来膨胀布局
然后尝试访问元素。
使用 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:尝试在
处的空对象引用上调用虚方法 'void android.widget.ProgressBar.setVisibility(int)'
选项:1 检查是否为空
protected void onPreExecute(){
if(progressBar != null)
progressBar.setVisibility(ProgressBar.VISIBLE);
button1.setEnabled(false);
super.onPreExecute();
}
和,
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
我正在尝试将我的代码更改为 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_.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);
}
}
}
@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); 来膨胀布局 然后尝试访问元素。
使用 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:尝试在
处的空对象引用上调用虚方法 'void android.widget.ProgressBar.setVisibility(int)'
选项:1 检查是否为空
protected void onPreExecute(){ if(progressBar != null) progressBar.setVisibility(ProgressBar.VISIBLE); button1.setEnabled(false); super.onPreExecute(); }
和,
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