运行应用时出现此错误:
05-10 01:44:39.243 11047-11047 / com.efe.efeAlumni E / AndroidRuntime:FATAL EXCEPTION:main进程:com.efe.efeAlumni,PID:11047 java.lang.RuntimeException:无法启动活动ComponentInfo {com.efe.efeAlumni / com.efe.efeAlumni.ui.MainActivity}:java.lang.NullPointerException:尝试在android.app的android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)获取null数组的长度android.app.Handler的android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)上的android.app.ActivityThread.access $ 800(ActivityThread.java:151)的.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) android.app.Looper.loop(Looper.java:135)的.dispatchMessage(Handler.java:102)位于android.app.ActivityThread.main(ActivityThread.java:5254)的java.lang.reflect.Method.invoke(原始方法)在com.android.internal.os的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)的java.lang.reflect.Method.invoke(Method.java:372)。 ZygoteInit.main (ZygoteInit.java:698)引起:java.lang.NullPointerException:尝试在android.widget.ListView.setAdapter的com.efe.efeAlumni.Adaptors.JobAdaptor.getCount(JobAdaptor.java:28)获取null数组的长度(ListView.java:487)位于android.app.A活动中的com.efe.efeAlumni.ui.MainActivity.onCreate(MainActivity.java:74)上的android.app.ListActivity.setListAdapter(ListActivity.java:265) Activity.java:5990)在Android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)的android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) )在Android.app.Handler.dispatchMessage(Handler.java:102)的android.app.ActivityThread.access $ 800(ActivityThread.java:151)android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303) android.os.Looper.loop(Looper.java:135)在android.app.ActivityThread.main(ActivityThread.java:5254)java.lang.reflect.Method.invoke(Native Method) )在com.android.internal.os.ZygoteInit的com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)的java.lang.reflect.Method.invoke(Method.java:372)中。主(ZygoteInit.java:698)
MainActivity:
import android.app.ListActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
package com.efe.efeAlumni.ui;
import android.app.ListActivity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import com.efe.efeAlumni.Adaptors.JobAdaptor;
import com.efe.efeAlumni.Model.Config;
import com.efe.efeAlumni.Model.JobDetails;
import com.efe.efeAlumni.Model.Jobs;
import com.efe.efeAlumni.R;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
public class MainActivity extends ListActivity {
public static final String TAG = MainActivity.class.getSimpleName();
private Jobs mJobs;
private JobDetails[] mJobDetails;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getJobsDetails();
JobAdaptor adaptor = new JobAdaptor(this, mJobDetails);
setListAdapter(adaptor);
Log.d(TAG," OkHttp success ");
}
private void getJobsDetails() {
if(isNetworkAvailable()) {
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(Config.jobsURL)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Request request, IOException e) {
}
@Override
public void onResponse(Response response) throws IOException {
try {
String jsonData = response.body().string();
Log.v(TAG, jsonData);
if (response.isSuccessful()) {
mJobs = parseJobsDetails(jsonData);
} else {
AlertUserAboutError();
}
} catch (IOException e) {
Log.v(TAG, "Exception caught: ", e);
} catch (JSONException e) {
Log.v(TAG, "Exception caught: ", e);
}
}
});
}else {
AlertUserAboutError();
}
}
private Jobs parseJobsDetails (String jsonData) throws JSONException{
Jobs jobs = new Jobs();
jobs.setJobDetailses(getCurrentJobDetails(jsonData));
return jobs;
}
private JobDetails[] getCurrentJobDetails(String jsonData) throws JSONException {
JSONObject jsonObject = new JSONObject(jsonData);
JSONArray result = jsonObject.getJSONArray("result");
JobDetails[] jobDetails = new JobDetails[result.length()];
for (int i = 0; i < result.length(); i++) {
JSONObject jsonDetail = result.getJSONObject(i);
JobDetails jobDetail = new JobDetails();
jobDetail.setIds(jsonDetail.getString("id"));
jobDetail.setSalary(jsonDetail.getString("title"));
jobDetail.setTitles(jsonDetail.getString("salary"));
jobDetails[i] = jobDetail;
Log.d(TAG, jobDetail.getIds());
}
return jobDetails;
}
private boolean isNetworkAvailable() {
ConnectivityManager manger = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manger.getActiveNetworkInfo();
boolean isAvailable = false;
if(networkInfo != null && networkInfo.isConnected()){
isAvailable = true;
}
return isAvailable;
}
private void AlertUserAboutError() {
AlertDialogFragment dialog = new AlertDialogFragment();
dialog.show(getFragmentManager(),"error_dailog");
}
}
JobAdaptor类:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.efe.efeAlumni.Model.JobDetails;
import com.efe.efeAlumni.R;
public class JobAdaptor extends BaseAdapter {
private JobDetails[] mJobs;
private Context mContext;
public JobAdaptor(Context context, JobDetails[] jobs) {
mContext = context;
mJobs = jobs;
}
@Override
public int getCount() {
return mJobs.length;
}
@Override
public Object getItem(int i) {
return mJobs[i];
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.job_list_items, null);
holder = new ViewHolder();
holder.titleLabel = (TextView) convertView.findViewById(R.id.titleTextView);
holder.salaryLabel = (TextView) convertView.findViewById(R.id.salaryTextView);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
JobDetails job = mJobs[position];
holder.titleLabel.setText(job.getTitles() + "");
holder.salaryLabel.setText(job.getSalary() + "");
if (position == 0) {
holder.titleLabel.setText("Noting to show ");
holder.titleLabel.setText(" ");
}
return convertView;
}
private static class ViewHolder {
TextView titleLabel;
TextView salaryLabel;
}
}
JobDetails类:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.efe.efeAlumni.Model.JobDetails;
import com.efe.efeAlumni.R;
public class JobAdaptor extends BaseAdapter {
private JobDetails[] mJobs;
private Context mContext;
public JobAdaptor(Context context, JobDetails[] jobs) {
mContext = context;
mJobs = jobs;
}
@Override
public int getCount() {
return mJobs.length;
}
@Override
public Object getItem(int i) {
return mJobs[i];
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
// brand new
convertView = LayoutInflater.from(mContext).inflate(R.layout.job_list_items, null);
holder = new ViewHolder();
holder.titleLabel = (TextView) convertView.findViewById(R.id.titleTextView);
holder.salaryLabel = (TextView) convertView.findViewById(R.id.salaryTextView);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
JobDetails job = mJobs[position];
holder.titleLabel.setText(job.getTitles() + "");
holder.salaryLabel.setText(job.getSalary() + "");
if (position == 0) {
holder.titleLabel.setText("Noting to show ");
holder.titleLabel.setText(" ");
}
return convertView;
}
private static class ViewHolder {
TextView titleLabel;
TextView salaryLabel;
}
}