2014-02-23 51 views
0

我的android应用程序用于从链接加载图像。它可以运行以显示activity_main。但是当我把链接和加载图像。它崩溃了。我检查了很多次。但我找不到错误。任何人都可以帮助我?致命异常主要nullpointerexception如何解决它?

public class MainActivity extends Activity { 
Button btt; 
EditText edtext; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    edtext = (EditText) this.findViewById(R.id.edtext); 
    btt = (Button) this.findViewById(R.id.btt); 
    btt.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      ImageView img; 
      String url= edtext.getText().toString(); 
      Bitmap bitmap = DownloadImage(url); 
      img = (ImageView) findViewById(R.id.imgview); 
      img.setImageBitmap(bitmap); 

     } 
    }); 
} 

private InputStream OpenHttpConnection(String urlString) throws IOException { 
    InputStream in = null; 
    int response = -1; 

    URL url = new URL(urlString); 
    URLConnection conn = url.openConnection(); 

    if(!(conn instanceof HttpURLConnection)) 
     throw new IOException("Not an http connection"); 
    try { 
     HttpURLConnection http = (HttpURLConnection) conn; 
     http.setAllowUserInteraction(false); 
     http.setInstanceFollowRedirects(true); 
     http.setRequestMethod("GET"); 
     http.connect(); 
     response = http.getResponseCode(); 
     if(response == HttpURLConnection.HTTP_OK) { 
      in = http.getInputStream(); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return in; 
} 
private Bitmap DownloadImage(String URL) { 
    Bitmap bitmap = null; 
    InputStream in = null; 
    try { 
    in = OpenHttpConnection(URL); 
    bitmap = BitmapFactory.decodeStream(in); 
    in.close(); 
    } catch (IOException e1) { 
     e1.printStackTrace(); 
    } 
    return bitmap; 
} 
@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

}

我mainactivity.java

<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=".MainActivity" > 

<EditText 
    android:id="@+id/edtext" 
    android:layout_width="300dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:ems="10" 
    android:text="http://" > 

    <requestFocus /> 
</EditText> 

<Button 
    android:id="@+id/btt" 
    style="?android:attr/buttonStyleSmall" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:text="Load" /> 

<ImageView 
    android:id="@+id/imgview" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/edtext" 
    android:layout_below="@+id/btt" 
    android:layout_marginTop="24dp" 
    android:src="@drawable/ic_launcher" /> 

activity_main.xml中

02-23 21:17:15.508: E/AndroidRuntime(1270): FATAL EXCEPTION: main 
02-23 21:17:15.508: E/AndroidRuntime(1270): java.lang.NullPointerException 
02-23 21:17:15.508: E/AndroidRuntime(1270):  at com.example.images.MainActivity.DownloadImage(MainActivity.java:75) 
02-23 21:17:15.508: E/AndroidRuntime(1270):  at com.example.images.MainActivity.access$0(MainActivity.java:69) 
02-23 21:17:15.508: E/AndroidRuntime(1270):  at com.example.images.MainActivity$1.onClick(MainActivity.java:37) 
02-23 21:17:15.508: E/AndroidRuntime(1270):  at android.view.View.performClick(View.java:4084) 
02-23 21:17:15.508: E/AndroidRuntime(1270):  at android.view.View$PerformClick.run(View.java:16966) 
02-23 21:17:15.508: E/AndroidRuntime(1270):  at android.os.Handler.handleCallback(Handler.java:615) 
02-23 21:17:15.508: E/AndroidRuntime(1270):  at android.os.Handler.dispatchMessage(Handler.java:92) 
02-23 21:17:15.508: E/AndroidRuntime(1270):  at android.os.Looper.loop(Looper.java:137) 
02-23 21:17:15.508: E/AndroidRuntime(1270):  at android.app.ActivityThread.main(ActivityThread.java:4745) 
02-23 21:17:15.508: E/AndroidRuntime(1270):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-23 21:17:15.508: E/AndroidRuntime(1270):  at java.lang.reflect.Method.invoke(Method.java:511) 
02-23 21:17:15.508: E/AndroidRuntime(1270):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
02-23 21:17:15.508: E/AndroidRuntime(1270):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
02-23 21:17:15.508: E/AndroidRuntime(1270):  at dalvik.system.NativeStart.main(Native Method) 
+0

哪一行是:'MainActivity.java:75'? – donfuxx

+1

好的,让我更清楚一点:在stackoverflow中没有显示源代码行号,并且需要分析这个问题,我需要知道这一行是什么:MainActivity:75.请问您可以发布它吗?我不想像其他人一样推测。 – donfuxx

回答

-1

img = (ImageView) findViewById(R.id.imgview); 

尝试更改为

View parent = (View)v.getParent(); 
if (parent != null) { 
    img = (ImageView) parent.findViewById(R.id.imgview); 
} 

我猜findViewById被返回null。

+0

你正在暗示他在Button里面寻找ImageView – Blackbelt

+0

对不起。你是对的。编辑 – JamesSugrue

-1

我的猜测来自您的追踪是OpenHttpConnection方法返回空输入流。

如果在日志中没有其他堆栈跟踪,然后就是跟你搞乱了一段代码是这一个:

if(response == HttpURLConnection.HTTP_OK) { 
    in = http.getInputStream(); 
} 

如果你想获得的图像是不存在的,你的InputStream将为null。当发生这种情况时,你需要考虑你想做什么(例如不设置图像?)。

我并不完全确定它,但是如果您的图像被缓存,您可能会得到与HTTP 200 OK不同的响应,例如,它可能是“304未修改”。