gpt4 book ai didi

android - NullPointerException imageView.setImageBitmap(位图)

转载 作者:行者123 更新时间:2023-11-29 21:09:02 26 4
gpt4 key购买 nike

我正在尝试从网站下载图片。这是我的代码:主要 Activity

...
public class MainActivity extends Activity {
public final static String URL = "com.example.test3.URL";
ImageView imageView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}

// execute by pressing button
public void send(View view){
EditText enter = (EditText) findViewById(R.id.enter);
String url = enter.getText().toString();
ImageView imageView = (ImageView) findViewById(R.id.image1);
new ImageDownload(imageView).execute(url);
}

}

图片下载

public class ImageDownload extends AsyncTask<String, Void, Bitmap>{
ImageView imageView = null;
public ImageDownload(ImageView imageView){
this.imageView = imageView;
}

/*@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_download);

if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Intent intent = getIntent();
String urlString = intent.getStringExtra(MainActivity.URL); */


@Override
protected Bitmap doInBackground(String... url) {
if(url.length <= 0){
return null;
}
return downloadBitmap(url[0]);
}

public Bitmap downloadBitmap(String s) {
String urlString = s;
URL url;
InputStream i;
Bitmap bitmap = null;
try {
url = new URL(urlString);
i = url.openStream();
bitmap = BitmapFactory.decodeStream(i);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}

return bitmap;
}

public void onPostExecute(Bitmap bitmap){
Log.d("done", "onPostExecute");
if(bitmap != null){
this.imageView.setImageBitmap(bitmap);
}
}
}

程序停止并显示以下日志:

05-08 09:52:04.477: D/done(8534): onPostExecute
05-08 09:52:04.477: D/AndroidRuntime(8534): Shutting down VM
05-08 09:52:04.477: W/dalvikvm(8534): threadid=1: thread exiting with uncaught exception (group=0x415e7d88)
05-08 09:52:04.497: E/AndroidRuntime(8534): FATAL EXCEPTION: main
05-08 09:52:04.497: E/AndroidRuntime(8534): Process: com.example.test3, PID: 8534
05-08 09:52:04.497: E/AndroidRuntime(8534): java.lang.NullPointerException
05-08 09:52:04.497: E/AndroidRuntime(8534): at com.example.test3.ImageDownload.onPostExecute(ImageDownload.java:90)
05-08 09:52:04.497: E/AndroidRuntime(8534): at com.example.test3.ImageDownload.onPostExecute(ImageDownload.java:1)
05-08 09:52:04.497: E/AndroidRuntime(8534): at android.os.AsyncTask.finish(AsyncTask.java:632)
05-08 09:52:04.497: E/AndroidRuntime(8534): at android.os.AsyncTask.access$600(AsyncTask.java:177)
05-08 09:52:04.497: E/AndroidRuntime(8534): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
05-08 09:52:04.497: E/AndroidRuntime(8534): at android.os.Handler.dispatchMessage(Handler.java:102)
05-08 09:52:04.497: E/AndroidRuntime(8534): at android.os.Looper.loop(Looper.java:212)
05-08 09:52:04.497: E/AndroidRuntime(8534): at android.app.ActivityThread.main(ActivityThread.java:5151)
05-08 09:52:04.497: E/AndroidRuntime(8534): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 09:52:04.497: E/AndroidRuntime(8534): at java.lang.reflect.Method.invoke(Method.java:515)
05-08 09:52:04.497: E/AndroidRuntime(8534): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
05-08 09:52:04.497: E/AndroidRuntime(8534): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
05-08 09:52:04.497: E/AndroidRuntime(8534): at dalvik.system.NativeStart.main(Native Method)
05-08 09:52:05.707: I/Process(8534): Sending signal. PID: 8534 SIG: 9

fragment_image_download.xml:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.test3.ImageDownload$PlaceholderFragment" >

<ImageView
android:contentDescription="@string/desc"
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</ImageView>

</RelativeLayout>

这个 NullPointerException 是从哪里来的?谁能帮帮我?

最佳答案

这里的问题是您错误地使用了 fragment 中的 Viewssend 函数是 Activity 类的成员,而不是 Fragment 类的成员,但是您的 ImageView 是在 中声明的code>Fragment,因此您应该在调用 findViewById 之前提供它的 Root View (不等于 Activity 的 Root View )。像这样:

ImageView imageView = getFragmentManager().findFragmentById(...).getView().findViewById(R.id.image1);

但最好的解决方案是将所有与 fragment 相关的逻辑移动到 Fragment 类中。

关于android - NullPointerException imageView.setImageBitmap(位图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23536365/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com