Я новичок в андроид, работаю над проектом в котором требуется получить из сервера JSON объекты и вывести в layout, в предыдущем активити я авторизируюсь и получаю токен для авторизации, передаю его с помощью Intent
вот мой код:
package com.example.sanzharaubakir.fin;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import java.io.IOException;
import cz.msebera.android.httpclient.HttpEntity;
import cz.msebera.android.httpclient.HttpResponse;
import cz.msebera.android.httpclient.client.HttpClient;
import cz.msebera.android.httpclient.client.methods.HttpGet;
import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
import cz.msebera.android.httpclient.util.EntityUtils;
/**
* Created by sanzharaubakir on 28.07.16.
*/
public class show extends Activity {
LinearLayout l;
As a;
String[] s;
String token;
int n = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_res);
LinearLayout linLayout = new LinearLayout(this);
linLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams linLayoutParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
setContentView(linLayout, linLayoutParam);
s = new String[1000];
Intent intent = getIntent();
token = intent.getStringExtra("token");
a.execute();
for (int i = 0 ; i < n; i++)
{
TextView v = new TextView(this);
v.setText(s[i]);
linLayout.addView(v, linLayoutParam);
final int finalI = i;
}
Button b = new Button(this);
b.setText("Сканировать");
linLayout.addView(b, linLayoutParam);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), BarcodeScanner.class);
intent.putExtra("token",token);
startActivity(intent);
}
});
}
public class As extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(getApplicationContext(), "1", Toast.LENGTH_LONG).show();
}
protected Void doInBackground(Void... voids) {
func();
return null;
}
}
public void func()
{
HttpClient httpclient = new DefaultHttpClient();
HttpGet get = new HttpGet("http://144.76.29.144:8001/warehouse/api/list/?access-token=<" + token + ">/");
try {
HttpResponse response = httpclient.execute(get);
HttpEntity httpEntity = response.getEntity();
String line = EntityUtils.toString(httpEntity, "UTF-8");
JSONArray arr = new JSONArray(line);
n = arr.length();
for(int i = 0; i < arr.length(); i++){
//s[i] = arr.getJSONObject(i).getString("items");
s[i] = arr.getJSONObject(i).toString();
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
и XML файл:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" 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"
android:background="@color/windowBackground">
</LinearLayout>
при вызове метода a.execute();
приложение крашится
вот что выдает Log
FATAL EXCEPTION: main
Process: com.example.sanzharaubakir.fin, PID: 17677
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sanzharaubakir.fin/com.example.sanzharaubakir.fin.show}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.sanzharaubakir.fin.show.onCreate(show.java:35)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
a = new As();
забылиreturn null
то есть вся ваша работа в отдельном потоке никуда не возвращается, что бы там не делалось - всегда будет null.