第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定

空指針錯(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();

正在回答

4 回答

如果提示報(bào)空指針的問(wèn)題說(shuō)明你對(duì)java還不了解,你這兩個(gè)對(duì)象沒(méi)有初始化,下面就用肯定空指針啊 progressBar=(ProgressBar)findViewById(R.id.progressBar); ? ? ? ?imageView=(ImageView)findViewById(R.id.imageView);

1 回復(fù) 有任何疑惑可以回復(fù)我~

MainActivity里面onCreate

???????Image a=new Image();
? ? ? ?Image.MyAsyncTask task=a.new MyAsyncTask();
? ? ? ?task.execute();

這個(gè)三行是想干啥?感覺(jué)像是打錯(cuò)了。去掉這三行。

0 回復(fù) 有任何疑惑可以回復(fù)我~

把報(bào)錯(cuò)信息說(shuō)詳細(xì)點(diǎn)

0 回復(fù) 有任何疑惑可以回復(fù)我~

報(bào)啥錯(cuò)你都不說(shuō)清楚

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報(bào)

0/150
提交
取消
Android必學(xué)-AsyncTask基礎(chǔ)
  • 參與學(xué)習(xí)       40898    人
  • 解答問(wèn)題       111    個(gè)

了解Android中AsyncTask的使用方法,掌握異步線程的使用

進(jìn)入課程

空指針錯(cuò)誤

我要回答 關(guān)注問(wèn)題
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)