java.lang.NullPointerException:Arraylist.size

时间:2017-01-25 08:41:09

标签: java android arraylist baseadapter parcelable

我想将arraylist发送到片段类,但是不能使用构造函数发送。然后尝试使用parcelable(第一次使用)和仍然错误(意图没有存储arraylist)。希望你们可以帮我解决这个问题。谢谢。

错误

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: net.simplifiedcoding.androidtablayout, PID: 20606
                  java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
                      at net.simplifiedcoding.androidtablayout.BaseAdapter.getCount(BaseAdapter.java:33)
                      at android.widget.ListView.setAdapter(ListView.java:487)
                      at net.simplifiedcoding.androidtablayout.Tab3.onCreateView(Tab3.java:46)
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1248)
                      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
                      at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                      at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
                      at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
                      at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:552)
                      at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:514)
                      at android.support.v4.view.ViewPager.setCurrent

MainActivity.java

package net.simplifiedcoding.androidtablayout;

import android.os.Bundle;
import android.os.StrictMode;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener{

    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //initialize toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.mipmap.ic_launcher_round);
        setSupportActionBar(toolbar);

        //initialize tablayout
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);

        //create create two tab and add those tab inside tablayout object (content not included,just tab)
        tabLayout.addTab(tabLayout.newTab().setText("Aduan"));
        tabLayout.addTab(tabLayout.newTab().setText("Semakan"));
        tabLayout.addTab(tabLayout.newTab().setText("Notis"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        //initialize viewpager
        viewPager = (ViewPager) findViewById(R.id.pager);


        //initialize fragment pager adapter
        Pager adapter = new Pager(getSupportFragmentManager(), tabLayout.getTabCount(),MainActivity.this);
        viewPager.setAdapter(adapter);
        tabLayout.setOnTabSelectedListener(this);

    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }
}

NotisBackgroundTask.java

package net.simplifiedcoding.androidtablayout.volley;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;

import net.simplifiedcoding.androidtablayout.MainActivity;
import net.simplifiedcoding.androidtablayout.Tab3;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by User on 1/24/2017.
 */

public class NotisBackgroundTask {
    Context context;
    ArrayList<Notis> arrayList = new ArrayList<>();


    public NotisBackgroundTask(Context context){
        this.context = context;
        callNotisFromServer();
        parcelableProcess();
    }

    public void callNotisFromServer(){

        // make json request (volley)

        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, "http://192.168.1.106/android/notis_android.php", (String) null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                //get all information from json
                int count = 0;
                while(count <response.length()){
                    try{
                        JSONObject jsonObject = response.getJSONObject(count);
                        Notis notis = new Notis(jsonObject.getString("time_mula"),
                                jsonObject.getString("time_tamat"),jsonObject.getString("dt_mula"),jsonObject.getString("dt_tamat"),
                                jsonObject.getString("notis_sebab"),jsonObject.getString("notis_lokasi"));
                        arrayList.add(notis);
                        count++;
                    }catch (JSONException e){
                        Log.d("message:",e.getMessage());
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("Volley ERROR:",error.getMessage());
            }
        });
        MySingleton.getmInstances(context).addRequestQueue(jsonArrayRequest);


    }


    public void parcelableProcess(){
       Intent intent = new Intent();
       intent.putParcelableArrayListExtra("arraylistNotis",this.getArrayList());

    }
    public ArrayList<Notis> getArrayList() {
        return arrayList;
    }

}

Tab3.java

package net.simplifiedcoding.androidtablayout;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import net.simplifiedcoding.androidtablayout.volley.Notis;
import net.simplifiedcoding.androidtablayout.volley.NotisBackgroundTask;

import java.util.ArrayList;

/**
 * Created by User on 1/24/2017.
 */

public class Tab3 extends Fragment {

    ArrayList<Notis> arrayList;
    ListView listView;
    Context context;

    public Tab3(){

    }

    @Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // return super.onCreateView(inflater, container, savedInstanceState);


        View view = inflater.inflate(R.layout.tab3,null);

        arrayList = getActivity().getIntent().getParcelableArrayListExtra("arraylistNotis");
        context  =  getActivity().getApplicationContext();

        listView = (ListView) view.findViewById(R.id.papar_notis_listview_id);
        listView.setAdapter(new BaseAdapter(context,arrayList));


        return view;
    }


}

BaseAdapter.java

package net.simplifiedcoding.androidtablayout;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import net.simplifiedcoding.androidtablayout.volley.Notis;

import org.w3c.dom.Text;

import java.util.ArrayList;

/**
 * Created by User on 1/25/2017.
 */

public class BaseAdapter extends android.widget.BaseAdapter {

    Context context;
    ArrayList<Notis> arrayList;
    private static LayoutInflater layoutInflater;

    public BaseAdapter(Context context, ArrayList<Notis> arrayList){
        this.context = context;
        this.arrayList = arrayList;
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public class Holder{
        TextView txt_time_mula;
        TextView txt_time_tamat;
        TextView txt_dt_mula;
        TextView txt_dt_tamat;
        TextView txt_notis_sebab;
        TextView getTxt_notis_lokasi;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final Notis notis = arrayList.get(position);
        Holder holder = new Holder();
        View rowView = layoutInflater.inflate(R.layout.notis_row_list,null);
        holder.txt_time_mula = (TextView) rowView.findViewById(R.id.txt_dt_mula_id);
        holder.txt_time_tamat = (TextView) rowView.findViewById(R.id.txt_time_tamat_id);
        holder.txt_dt_mula = (TextView) rowView.findViewById(R.id.txt_dt_mula_id);
        holder.txt_dt_tamat = (TextView) rowView.findViewById(R.id.txt_dt_tamat_id);
        holder.txt_notis_sebab = (TextView) rowView.findViewById(R.id.txt_notis_sebab_id);
        holder.getTxt_notis_lokasi = (TextView) rowView.findViewById(R.id.txt_notis_lokasi_id);

        holder.txt_time_mula.setText(notis.getTime_mula());
        holder.txt_time_tamat.setText(notis.getTime_tamat());
        holder.txt_dt_mula.setText(notis.getDt_mula());
        holder.txt_dt_tamat.setText(notis.getDt_tamat());
        holder.txt_notis_sebab.setText(notis.getNotis_sebab());
        holder.getTxt_notis_lokasi.setText(notis.getNotis_lokasi());

        rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context,"your click"+notis.getNotis_sebab(),Toast.LENGTH_SHORT).show();
            }
        });

        return rowView;
    }
}

0 个答案:

没有答案