空指針錯(cuò)誤
public class MainActivity extends AppCompatActivity {
? ?@Override
? ?protected void onCreate(Bundle savedInstanceState) {
? ? ? ?super.onCreate(savedInstanceState);
? ? ? ?setContentView(R.layout.activity_main);
? ? ? ?Image a=new Image();
? ? ? ?Image.MyAsyncTask task=a.new MyAsyncTask();
? ? ? ?task.execute();
? ?}
? ?public void loadimage(View view) {
? ? ? ?startActivity(new Intent(this,Image.class));
? ?}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ?android:layout_width="match_parent"
? ?android:layout_height="match_parent"
? ?android:orientation="vertical"
>
<Button
? ?android:onClick="loadimage"
? ?android:layout_width="match_parent"
? ?android:layout_height="wrap_content"
? ?android:text="loading image"
? ?/>
</LinearLayout>
public class Image extends Activity {
? ?private ImageView imageView;
? ?private ProgressBar progressBar;
? ?//圖片地址
? ?private static String URL = "http://pic.qiantucdn.com/58pic/19/43/68/56d3e7ffb7957_1024.jpg";
? ?@Override
? ?protected void onCreate(Bundle savedInstanceState) {
? ? ? ?super.onCreate(savedInstanceState);
? ? ? ?setContentView(R.layout.image);
? ? ? ?imageView = (ImageView) findViewById(R.id.image);
? ? ? ?progressBar = (ProgressBar) findViewById(R.id.progressBar);
? ? ? ?new MyAsyncTask().execute(URL); ?//設(shè)置傳遞進(jìn)去的參數(shù)
? ?}
? ?class MyAsyncTask extends AsyncTask<String, Void, Bitmap> {
? ? ? ?@Override
? ? ? ?protected void onPreExecute() {
? ? ? ? ? ?super.onPreExecute();
? ? ? ? ? ?progressBar.setVisibility(View.VISIBLE); ?//顯示進(jìn)度條
? ? ? ?}
? ? ? ?@Override
? ? ? ?protected void onPostExecute(Bitmap bitmap) {
? ? ? ? ? ?super.onPostExecute(bitmap);
? ? ? ? ? ?progressBar.setVisibility(View.GONE); ?//隱藏進(jìn)度條顯示圖片
? ? ? ? ? ?imageView.setImageBitmap(bitmap);
? ? ? ?}
? ? ? ?@Override
? ? ? ?protected Bitmap doInBackground(String... params) { ? //String... params可變長(zhǎng)數(shù)組
? ? ? ? ? ?String url = params[0]; ? ? ? ?//獲取傳遞進(jìn)來(lái)的參數(shù)
? ? ? ? ? ?Bitmap bitmap = null;
? ? ? ? ? ?URLConnection connection; ? //定義網(wǎng)絡(luò)連接對(duì)象
? ? ? ? ? ?InputStream is; ? ? ? ? ? //用于獲取數(shù)據(jù)的輸入的數(shù)據(jù)流
? ? ? ? ? ?try {
? ? ? ? ? ? ? ?connection = new URL(url).openConnection(); ? //獲取網(wǎng)絡(luò)連接對(duì)象
? ? ? ? ? ? ? ?is = connection.getInputStream(); ?//獲取輸入流
? ? ? ? ? ? ? ?BufferedInputStream bis = new BufferedInputStream(is);
? ? ? ? ? ? ? ?bitmap = BitmapFactory.decodeStream(bis); ?//將輸入流解析成bitmap
? ? ? ? ? ? ? ?is.close();
? ? ? ? ? ? ? ?bis.close();
? ? ? ? ? ?} catch (IOException e) {
? ? ? ? ? ? ? ?e.printStackTrace();
? ? ? ? ? ?}
? ? ? ? ? ?return bitmap;
? ? ? ?}
? ?}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ?android:layout_width="match_parent"
? ?android:layout_height="match_parent">
? ?<ImageView
? ? ? ?android:id="@+id/image"
? ? ? ?android:layout_width="match_parent"
? ? ? ?android:layout_height="match_parent" />
? ?<ProgressBar
? ? ? ?android:id="@+id/progressBar"
? ? ? ?android:layout_width="wrap_content"
? ? ? ?android:layout_height="wrap_content"
? ? ? ?android:layout_centerInParent="true"
? ? ? ?android:visibility="gone" />
</RelativeLayout>
activity都注冊(cè)了
報(bào)錯(cuò)在這2行
Image 里的progressBar.setVisibility(View.VISIBLE); ?//顯示進(jìn)度條
MainActivity 里的task.execute();
2016-05-31
如果提示報(bào)空指針的問(wèn)題說(shuō)明你對(duì)java還不了解,你這兩個(gè)對(duì)象沒(méi)有初始化,下面就用肯定空指針啊 progressBar=(ProgressBar)findViewById(R.id.progressBar); ? ? ? ?imageView=(ImageView)findViewById(R.id.imageView);
2016-04-07
MainActivity里面onCreate
???????Image a=new Image();
? ? ? ?Image.MyAsyncTask task=a.new MyAsyncTask();
? ? ? ?task.execute();
這個(gè)三行是想干啥?感覺(jué)像是打錯(cuò)了。去掉這三行。
2016-04-07
把報(bào)錯(cuò)信息說(shuō)詳細(xì)點(diǎn)
2016-04-07
報(bào)啥錯(cuò)你都不說(shuō)清楚