我想知道网址中是否存在文件的应用不断停止

时间:2018-12-08 06:21:52

标签: android

我正在尝试编写一个应用程序,如果给定的URL中有文件,则将一个字符串放入textview中,这是代码和崩溃信息,这可能是什么错误?

public class MainActivity extends AppCompatActivity {

String url = "https://images.pexels.com/photos/617278/pexels-photo-617278.jpeg?cs=srgb&dl=adorable-animal-blur-617278.jpg&fm=jpg";

TextView textView;

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

    textView.findViewById(R.id.textView);

        try {
            HttpURLConnection.setFollowRedirects(false);
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setRequestMethod("HEAD");
            connection.connect();

            if(connection.getResponseCode() == HttpURLConnection.HTTP_OK){

                textView.setText("File Exists!!");

            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
  

---------崩溃开始   E / AndroidRuntime:致命异常:main       流程:com.example.android.archivoexiste,PID:6160       java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.android.archivoexiste / com.example.android.archivoexiste.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'android.view.View android。在空对象引用上的widget.TextView.findViewById(int)'           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)           在android.app.ActivityThread.-wrap11(未知来源:0)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593)           在android.os.Handler.dispatchMessage(Handler.java:105)           在android.os.Looper.loop(Looper.java:164)           在android.app.ActivityThread.main(ActivityThread.java:6541)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)        原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ android.view.View android.widget.TextView.findViewById(int)”           在com.example.android.archivoexiste.MainActivity.onCreate(MainActivity.java:23)           在android.app.Activity.performCreate(Activity.java:6975)           在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)           在android.app.ActivityThread.-wrap11(未知来源:0)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1593)           在android.os.Handler.dispatchMessage(Handler.java:105)           在android.os.Looper.loop(Looper.java:164)           在android.app.ActivityThread.main(ActivityThread.java:6541)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)   应用已终止。

1 个答案:

答案 0 :(得分:1)

在您的oncreate方法中更改此行

 textView.findViewById(R.id.textView);

对此

textView = (TextView) findViewById(R.id.textView);