我知道这个答案已在某些页面上被提及并回答过,例如这个页面。 Android App crashes after Scrolling down ListView
但我尝试了答案,我的应用程序仍会崩溃,并给我一个空指针异常,我似乎无法找到我应该把if语句放在哪里或者如果我需要做一些完全不同的事情我的代码
package com.evanarendssgmail.php;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends ListActivity {
private EditText name;
private EditText buzz;
private String [] names;
private JSONObject jsonResponse;
private String [] newNames;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String [] evan = new String[10];
for (int i = 0; i < 10; i ++) {
evan[i] = "evan";
}
jsonResponse = null;
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String TAG = "YOUR TAG";
Log.d(TAG,"after onResponse");
try {
Log.d(TAG,"after try");
jsonResponse = new JSONObject(response);
Log.d(TAG,"after object initialization");
boolean success = jsonResponse.getBoolean("success");
Log.d(TAG,"after boolean statement");
if (success) {
Log.d(TAG,"Sucess");
//jarray = jsonResponse.getJSONArray("name");
if (jsonResponse != null) {
String lengt = Integer.toString(jsonResponse.length());
Log.d(TAG, "after lengt intialization");
int length = Integer.parseInt(lengt);
Log.d(TAG, "After length initialization");
names = new String[length];
Log.d(TAG, "after array initialization");
String number = "";
for (int i = 0; i < length - 1; i++) {
Log.d(TAG, "beginning loop");
number = Integer.toString(i);
Log.d(TAG, "mid loop");
names[i] = jsonResponse.getString(number);
Log.d(TAG, "End loop");
}
}
Log.d(TAG,"Before array adapter");
Log.d(TAG,"End of Tag");
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Login Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
Log.d(TAG,"catch");
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest(responseListener);
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
queue.add(loginRequest);
//trnsfer();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getListView().getContext(),android.R.layout.simple_list_item_1,names);
getListView().setAdapter(adapter);
name = (EditText) findViewById(R.id.name_text);
buzz = (EditText) findViewById(R.id.buzz_text);
}
public void trnsfer() {
}
public void send_click(View v) {
String nme = name.getText().toString();
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");
if (success) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
MainActivity.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Register Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
RegisterRequest registerRequest = new RegisterRequest(nme, responseListener);
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
queue.add(registerRequest);
}
public void reap() {
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
String TAG = "YOUR TAG";
Log.d(TAG,"after onResponse");
try {
Log.d(TAG,"after try");
JSONObject jsonResponse = new JSONObject(response);
Log.d(TAG,"after object initialization");
boolean success = jsonResponse.getBoolean("success");
Log.d(TAG,"after boolean statement");
if (success) {
Log.d(TAG,"Sucess");
//jarray = jsonResponse.getJSONArray("name");
String lengt = Integer.toString(jsonResponse.length());
Log.d(TAG,"after lengt intialization");
int length = Integer.parseInt(lengt);
Log.d(TAG,"After length initialization");
names = new String[length];
Log.d(TAG,"after array initialization");
String number = "";
for (int i = 0; i < length - 1; i ++ ) {
Log.d(TAG,"beginning loop");
number = Integer.toString(i);
Log.d(TAG,"mid loop");
names[i] = jsonResponse.getString(number);
Log.d(TAG,"End loop");
}
Log.d(TAG,"Before array adapter");
Log.d(TAG,"End of Tag");
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("Login Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
Log.d(TAG,"catch");
e.printStackTrace();
}
}
};
LoginRequest loginRequest = new LoginRequest(responseListener);
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
queue.add(loginRequest);
}
}
--- XML FILES
<?xml version="1.0" encoding="utf-8"?>
<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="com.evanarendssgmail.php.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="250dp"
android:id="@android:id/list"
android:layout_below="@+id/buzz_text"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/name_text"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@android:id/list"
android:layout_alignEnd="@android:id/list"
android:text="Name" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buzz_text"
android:layout_below="@+id/name_text"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/name_text"
android:layout_alignEnd="@+id/name_text"
android:text="buzzword" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="@+id/button"
android:onClick="send_click"
android:layout_below="@android:id/list"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Receve"
android:onClick="receve_click"
android:id="@+id/button2"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
如果我将响应监听器放入onclick函数,它会将所有数据加载到listview中,但是一旦我滚动到底部,它将给我一个空指针异常。