无法运行app android java.lang.NullPointerException

时间:2014-06-25 13:31:55

标签: java android android-volley

我几天前开始学习Android,而且我一直在开发这个应用程序,它的功能基本上是使用凌空连接到API,但是当我尝试在智能手机中运行应用程序时出现以下错误:

-25 10:17:46.851  24476-24476/com.representemais E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.rep, PID: 24476
    java.lang.NullPointerException
            at com.rep.app.principal.InicioActivity$AutenticacaoLocalTask$1.onResponse(InicioActivity.java:89)
            at com.representemais.app.principal.InicioActivity$AutenticacaoLocalTask$1.onResponse(InicioActivity.java:84)
            at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65)
            at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5102)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)

这是InicioActivity:

package com.rep.app.principal;

import android.os.AsyncTask;
import android.os.Bundle;

import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.rep.R;


import org.json.JSONObject;

import java.util.HashMap;

import java.util.Map;


public class InicioActivity extends SherlockFragmentActivity {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        AutenticacaoLocalTask mAutenticacaoLocalTask = new AutenticacaoLocalTask();
        mAutenticacaoLocalTask.execute((Void) null);

    }
    private TextView txtDisplay;
    RequestQueue queue = Volley.newRequestQueue(this);

    public class AutenticacaoLocalTask extends AsyncTask<Void, Void, Boolean> {

        @Override
        protected Boolean doInBackground(Void... params) {


            try {




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

                String url = "http://192.168.1.15/rep-api/api/clients";







                 JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                        url, null,
                        new Response.Listener<JSONObject>() {

                            @Override
                            public void onResponse(JSONObject response) {

                                txtDisplay.setText("Response => "+response.toString());
                                findViewById(R.id.progressBar1).setVisibility(View.GONE);
                            }
                        }, new Response.ErrorListener() {



                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d("Error: " + error.getMessage());


                    }
                })



                {

                    @Override
                    public Map<String, String> getHeaders() throws AuthFailureError {

                        HashMap<String, String> headers = new HashMap<String, String>();


                        headers.put("X-API-TOKEN", "99KI9Gj68CgCf70deM22Ka64chef2C40Gm2lFJ2J0G9JkD0bDAcbFfd19MfacGf3FFm8CM1hG0eDiIk8");

                        return headers;
                    }



                };
                queue.add(jsonObjReq);


                return true;

            } catch (Exception e) {
                Log.e("RM", e.getMessage());
                return false;
            }

        }

        @Override
        protected void onPostExecute(final Boolean success) {

        }

        @Override
        protected void onCancelled() {

        }
    }

和Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rep"
    android:versionCode="1"
    android:versionName="1.0">

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">

        <activity
            android:name=".app.principal.InicioActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".app.login.LoginActivity"
            android:configChanges="keyboardHidden"
            android:label="@string/app_name">
        </activity>

        <activity
            android:name=".app.principal.MainActivity"
            android:label="@string/app_name"></activity>

        <activity android:name=".app.cliente.ClienteDetalheActivity"
            android:label="@string/app_name"></activity>

        <activity android:name=".app.login.LoginTelaBloqueada"
            android:label="@string/app_name"></activity>
    </application>



</manifest>

2 个答案:

答案 0 :(得分:4)

我认为你忘了

 setContentView(R.layout.your_layout);

您应该在onCreate(...)方法中设置布局,然后引用TextView

正确:

  @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.your_layout);
    txtDisplay = (TextView) findViewById(R.id.txtDisplay);
     .........
     ........
 }

答案 1 :(得分:1)

快速浏览一下,看起来您没有设置内容视图,这会导致findViewById返回null。试图在不存在的TextView中设置文本似乎是导致崩溃的直接原因。