我在Android Studio上开发的应用程序android发生崩溃,在日志中,我收到此错误消息(logs):

04-26 16:58:12.393 31024-31024/com.example.nicol.etablissement43 D/AbsListView: checkAbsListViewlLogProperty get invalid command
04-26 16:58:12.394 31024-31024/com.example.nicol.etablissement43 D/AndroidRuntime: Shutting down VM
04-26 16:58:12.395 31024-31024/com.example.nicol.etablissement43 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                   Process: com.example.nicol.etablissement43, PID: 31024
                                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nicol.etablissement43/com.example.nicol.etablissement43.recherche_2}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)
                                                                                       at android.app.ActivityThread.access$800(ActivityThread.java:178)
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                                       at android.os.Looper.loop(Looper.java:194)
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5631)
                                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)
                                                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
                                                                                       at com.example.nicol.etablissement43.recherche_2.onCreate(recherche_2.java:74)
                                                                                       at android.app.Activity.performCreate(Activity.java:6092)
                                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
                                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
                                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595) 
                                                                                       at android.app.ActivityThread.access$800(ActivityThread.java:178) 
                                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470) 
                                                                                       at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                                       at android.os.Looper.loop(Looper.java:194) 
                                                                                       at android.app.ActivityThread.main(ActivityThread.java:5631) 
                                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                                       at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) 
                                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 

这是我的课:
   package com.example.nicol.etablissement43;

import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by nicol on 25/04/2017.
 */

public class recherche_2 extends AppCompatActivity {
    private static final String LOG_TAG = "Selection Ville";
    private ListView Ville;
    private ArrayAdapter<String> listAdapter ;
    private AdapterClient adapterClient ;
    private TextView SQLData;

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

    protected void onCreate(Bundle savedInstanceState) {
        {

            if (Build.VERSION.SDK_INT > 8) {
                StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
                StrictMode.setThreadPolicy(policy);
            }

            super.onCreate(savedInstanceState);
            String page = this.getPage("http://appec43.890m.com/connectionbddandroid/config.inc.php");

            Log.i("ContenuPost", page);
            setContentView(R.layout.recherche_2);
            Ville = (ListView) findViewById(R.id.Ville);
            SQLData.setText("Les villes");
            SQLData.setText("Les villes");
            //String[] planets = new String[] {""};


            // ArrayList<String> planetList = new ArrayList<String>();
            // planetList.addAll( Arrays.asList(planets) );

            //listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, planetList);
            adapterClient = new AdapterClient(this, R.layout.recherche_2);
            try {
                JSONArray jsonArray = new JSONArray(page);

                for (int i = 0; i < jsonArray.length(); i++) {

                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    client clientobj = new client();
                    clientobj.setIdVille(jsonObject.getString("Ville"));
                    clientobj.setCodepostal(jsonObject.getString("codepostal"));

                    adapterClient.add(clientobj);
                }

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


        /*
        String[] split = page.split(",");
        for (int i = 0; i < split.length; i++) {
            listAdapter.add( split[i] );
            if (i != split.length - 1) {
                listAdapter.add(" ");
            }
        }
        */


           Ville.setAdapter(adapterClient);

        }
    }

    public String getPage(String adresse){

        StringBuffer stringBuffer = new StringBuffer("");
        BufferedReader bufferedReader = null;

        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 15000);

            HttpPost httpPost = new HttpPost();

            URI uri = new URI(adresse);

            httpPost.setURI(uri);
            List<NameValuePair> parametres = new ArrayList<NameValuePair>();
            parametres.add(new BasicNameValuePair("login", "u265740438_root2"));
            parametres.add(new BasicNameValuePair("pass", "nicolas"));
            UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parametres);
            httpPost.setEntity(formEntity);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            InputStream inputStream = httpResponse.getEntity().getContent();
            bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            String ligneLue = bufferedReader.readLine();

            while (ligneLue != null) {
                Log.i("Valeur", ligneLue);
                stringBuffer.append(ligneLue);
                stringBuffer.append("\n");
                ligneLue = bufferedReader.readLine();

            }
        }
        catch(Exception e) {
            Log.e("Erreur", e.getMessage());
        }
        finally{
            if(bufferedReader != null){
                try{
                    bufferedReader.close();
                }
                catch (IOException e){

                }
            }

        }

        return stringBuffer.toString();

    }


    }

这是我的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <TextView
        android:text="Villes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/SQLData"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="52dp"
        android:layout_marginStart="52dp" />

    <ListView
        android:id="@+id/Ville"
        style="@style/Widget.AppCompat.Light.ListView.DropDown"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/textView"
        android:layout_marginTop="24dp" />

有人会找到解决方案,因为我一直在寻找答案已经有一段时间了
先感谢您

最佳答案

您需要初始化SQLData textView。

像这样
SQLData = (TextView)findViewById(R.id.SQLData)方法中的onCreate

10-08 07:09