Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/192.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android “后黑屏”;开放式;新活动_Android - Fatal编程技术网

Android “后黑屏”;开放式;新活动

Android “后黑屏”;开放式;新活动,android,Android,首先,我必须说,我在开发方面是个新手,所以请原谅我提出的可能很愚蠢的问题。 我有两项活动。在MainActivity中,我有一个按钮,可以将我带到SecondActivity。触摸这个按钮后,我的手机出现了黑屏。哪里有问题?如果你想告诉我一个问题,我将非常感激。对不起我的英语。 下面是一个代码: 舱单: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.c

首先,我必须说,我在开发方面是个新手,所以请原谅我提出的可能很愚蠢的问题。 我有两项活动。在MainActivity中,我有一个按钮,可以将我带到SecondActivity。触摸这个按钮后,我的手机出现了黑屏。哪里有问题?如果你想告诉我一个问题,我将非常感激。对不起我的英语。 下面是一个代码:

舱单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ca_ltd.intenty">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".SecondActivity"></activity>
</application>

</manifest>
主要内容:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.ca_ltd.intenty.MainActivity"
tools:showIn="@layout/activity_main">

<Button
    android:id="@+id/buttonNext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Przejdź do następnego"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginBottom="8dp"
    android:layout_marginRight="8dp"
    app:layout_constraintRight_toRightOf="parent"
    android:layout_marginLeft="8dp"
    app:layout_constraintLeft_toLeftOf="parent" />
</android.support.constraint.ConstraintLayout>

第二项活动:

public class SecondActivity extends Activity {

ListView lv;
ArrayAdapter<String> adapter;
String address="http://10.0.0.2/soccer.php";
InputStream is=null;
String line=null;
String result=null;
String[] data;

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

    lv=(ListView) findViewById(R.id.listView1);

    StrictMode.setThreadPolicy((new StrictMode.ThreadPolicy.Builder().permitNetwork().build()));
    getData();
    adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
    lv.setAdapter(adapter);

}

private void getData()           
{
    try {
        URL url = new URL(address);
        HttpURLConnection con=(HttpURLConnection) url.openConnection();

        con.setRequestMethod("GET");

        is=new BufferedInputStream(con.getInputStream());

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

    try
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(is));
        StringBuilder sb=new StringBuilder();

        while((line=br.readLine()) != null);
        {
            sb.append(line+"\n");
        }
        is.close();
        result=sb.toString();

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

    try
    {
        JSONArray ja=new JSONArray(result);
        JSONObject jo=null;

        data=new String[ja.length()];

        int i;
        for (i = 0; i<ja.length();i++)
        {
            jo=ja.getJSONObject(i);
            data[i]=jo.getString("Name");
        }
    }catch (Exception e)
    {
        e.printStackTrace();
    }
}
}
公共类SecondActivity扩展活动{
ListView lv;
阵列适配器;
字符串地址=”http://10.0.0.2/soccer.php";
InputStream=null;
字符串行=null;
字符串结果=null;
字符串[]数据;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_秒);
lv=(ListView)findViewById(R.id.listView1);
StrictMode.setThreadPolicy((新的StrictMode.ThreadPolicy.Builder().permitNetwork().build());
getData();
adapter=new ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,数据);
低压设置适配器(适配器);
}
私有void getData()
{
试一试{
URL=新的URL(地址);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.setRequestMethod(“GET”);
is=新的BufferedInputStream(con.getInputStream());
}捕获(例外e)
{
e、 printStackTrace();
}
尝试
{
BufferedReader br=新的BufferedReader(新的InputStreamReader(is));
StringBuilder sb=新的StringBuilder();
而((line=br.readLine())!=null);
{
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e)
{
e、 printStackTrace();
}
尝试
{
JSONArray ja=新JSONArray(结果);
JSONObject jo=null;
数据=新字符串[ja.length()];
int i;

对于(i=0;i为什么要在主线程上进行网络调用。getData()方法应该在后台线程中执行,请使用异步任务或普通线程。此网络任务会消耗UI线程,这就是为什么会出现黑屏,如果超过5-6秒,将导致ANR

下面是一个使用截击进行请求的示例

// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // Parse your response here
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        // Error has occurred
    }
});

// Add the request to the RequestQueue.
queue.add(stringRequest);
//实例化RequestQueue。
RequestQueue=Volley.newRequestQueue(this);
字符串url=”http://www.google.com";
//从提供的URL请求字符串响应。
StringRequest StringRequest=新的StringRequest(Request.Method.GET,url,
新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
//在这里解析您的响应
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
//发生了错误
}
});
//将请求添加到RequestQueue。
添加(stringRequest);

有关截取的更多信息,请访问此页面。

您的应用程序正在崩溃,因为当您初始化适配器时,您的数据为空。作为解决此问题的第一步,您可以在获得api结果并初始化数据变量后创建适配器。

发布Logcat中显示的错误?@Raj I add Logcat。我这样做,因为使用它对我来说很容易-我真的很乞讨;)但我知道,我应该像你说的那样做。这就是我的应用程序崩溃的原因?我从Logcat添加了错误。是的,只要使用任何后台线程,它就会工作。或者最好使用任何网络库,如volley,Reformation。你的Logcat表明你的应用程序正在崩溃,因为数据变量为null。请确保请求成功
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    ></ListView>
</LinearLayout>
  --------- beginning of crash
    06-29 19:13:53.721 2831-2831/com.example.ca_ltd.intenty E/AndroidRuntime: 
    FATAL EXCEPTION: main

    Process: com.example.ca_ltd.intenty, PID: 2831

    java.lang.RuntimeException: Unable to start activity 

 ComponentInfo{com.example.ca_ltd.intenty/com.example.ca_ltd.intenty.SecondActivity}: java.lang.NullPointerException
                                                                              at 
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                                                                              at 
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                                                                              at 
    android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                              at 
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                                                                              at 
    android.os.Handler.dispatchMessage(Handler.java:105)
                                                                              at 
    android.os.Looper.loop(Looper.java:164)
                                                                              at 
    android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                              at 
    java.lang.reflect.Method.invoke(Native Method)
                                                                              at 
    com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                              at 
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

    Caused by: java.lang.NullPointerException
                                                                              at 
    java.util.Objects.requireNonNull(Objects.java:203)
                                                                              at 
    java.util.Arrays$ArrayList.<init>(Arrays.java:3741)
                                                                              at 
    java.util.Arrays.asList(Arrays.java:3728)
                                                                              at 
    android.widget.ArrayAdapter.<init>(ArrayAdapter.java:163)
                                                                              at 
    com.example.ca_ltd.intenty.SecondActivity.onCreate(SecondActivity.java:44)
                                                                              at 
    android.app.Activity.performCreate(Activity.java:6975)
                                                                              at 
    android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                                                                              at 
    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
                                                                              at 
    android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                                                                              at 
    android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                              at 
    android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                                                                              at 
    android.os.Handler.dispatchMessage(Handler.java:105) 
                                                                              at 
    android.os.Looper.loop(Looper.java:164) 
                                                                              at 
    android.app.ActivityThread.main(ActivityThread.java:6541) 
                                                                              at 
    java.lang.reflect.Method.invoke(Native Method) 
                                                                              at 
    com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                                                                              at 
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";

// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
            new Response.Listener<String>() {
    @Override
    public void onResponse(String response) {
        // Parse your response here
    }
}, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        // Error has occurred
    }
});

// Add the request to the RequestQueue.
queue.add(stringRequest);