我正试图通过Android应用程序从互联网上下载一些图像。我刚刚开始学习Android编程,现在我遇到了一些问题:下载现在正在运行,但我也想显示一个进度条并用BroadcastReceiver更新它。当我启动我的应用程序并想要使用sendBroadcast(Intent intent)更新时,我得到一个NullPointerException。 这是Logcat:
15815-16631/com.example.download_images E/AndroidRuntime: FATAL EXCEPTION: Thread-4729
Process: com.example.download_images, PID: 15815
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.content.Context.sendBroadcast(android.content.Intent)' on a null object reference
at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:387)
at com.example.download_images.ButtonService$3.run(ButtonService.java:106)
at java.lang.Thread.run(Thread.java:818)
这是我在MainActivity中的代码:
public class MainActivity extends AppCompatActivity{
ButtonService bs = new ButtonService();
IBinder service;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView) findViewById(R.id.textView);
final ImageView[] imgView = new ImageView[5];
imgView[0] = (ImageView) findViewById(R.id.imageView1);
final Button goB = (Button) findViewById(R.id.go);
final ProgressBar[] pb = new ProgressBar[5];
pb[0] = (ProgressBar) findViewById(R.id.progressBar1);
goB.setOnClickListener(bs.go);
BroadcastReceiver br = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//ButtonService.LocalBinder binder = (ButtonService.LocalBinder) service;
int progress = intent.getIntExtra("PROGRESS", 0);
int imgNum = intent.getIntExtra("IMG_NUMBER",0);
pb[imgNum].setProgress(progress);
boolean finished = intent.getBooleanExtra("FINISHED", false);
if (finished) {
Bitmap bitmap = ButtonService.bitMap[imgNum];
imgView[imgNum].setImageBitmap(bitmap);
pb[imgNum].setVisibility(View.GONE);
imgView[imgNum].setVisibility(View.VISIBLE);
}
}
};
IntentFilter filter = new IntentFilter("com.example.MY_ACTION");
this.registerReceiver(br, filter);
}
}
以下是我服务的代码:
package com.example.download_images;
public class ButtonService extends Service {
public static final Bitmap[] bitMap = new Bitmap[5];
Intent intent = new Intent();
public ButtonService() {}
View.OnClickListener go = new View.OnClickListener() {
public void onClick(View v){
Bitmap[] bilder = new Bitmap[5];
int i = 0;
//for (int i = 0; i < bilder.length; i++) {
bilder[i] = download("https://www.pokewiki.de/images/greenchu_willkommen2.png", i); // just a random URL to test
//}
}
};
public Bitmap download(final String src, final int i) {
final File[] bild = new File[5];
final int imgNum = i;
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = new BufferedInputStream(connection.getInputStream(),8192);
File ordner = new File(Environment.getExternalStorageDirectory(),"Download");
bild[imgNum] = new File(ordner,"bild"+imgNum+".png");
OutputStream output = new FileOutputStream(bild[imgNum]);
int size = connection.getContentLength();
int downloadedSize = 0;
byte[] buffer = new byte[1024];
int count;
while ((count = input.read(buffer)) != -1 )
{
output.write(buffer, 0, count);
downloadedSize += count;
int percent = 100 * downloadedSize/size;
intent.setAction("com.example.MY_ACTION");
intent.putExtra("PROGRESS", percent);
intent.putExtra("IMG_NUMBER",imgNum);
sendBroadcast(intent); // The NullpointerException is appearing here
}
output.flush();
output.close();
input.close();
bitMap[imgNum] = BitmapFactory.decodeFile(bild[imgNum].getAbsolutePath());
intent.putExtra("FINISHED",true);
intent.putExtra("IMG_NUMBER", imgNum);
sendBroadcast(intent);
} catch (IOException e) {
e.printStackTrace();
Log.e("Error: ", "Fehler!");
}
}
}).start();
return bitMap[imgNum];
}
}
抱歉,这是很多代码。 我知道NullPointerException是什么,但我不知道我在哪里访问null值。谁能告诉我它在哪里以及最终如何解决它?