java.lang.NullPointerException:尝试写入null数组

时间:2016-04-03 10:05:06

标签: java android

当我尝试初始化我的按钮btnEdit,btnDelete,btnView我得到了hull数组错误我知道问题是初始化但我不知道究竟在哪里,有人可以帮忙吗?我知道问题在于初始化,但我不知道究竟在哪里,有人可以帮忙吗?

here is my code:

package com.astro.famouspandit.Activities.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.astro.famouspandit.Database.DatabaseHelper;
import com.astro.famouspandit.R;
import com.astro.famouspandit.datasets.ItemProfile;

import java.util.ArrayList;

import mehdi.sakout.fancybuttons.FancyButton;

public class ProfleList extends AppCompatActivity implements View.OnClickListener{
    private TextView txtProfileName,txtProfileDate,txtProfileTime,txtProfileLocation;
    private FancyButton[] btnEdit,btnDelete,btnView;
    LinearLayout linearlist ;
    long count;

    DatabaseHelper mydb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profle_list);


        mydb = new DatabaseHelper(this);
        count = mydb.getCount();
        Log.d("count", "onCreate: "+count);

        if (count == 0){

        }else{
            ArrayList<ItemProfile> listprofile = mydb.profilelist();
            for (int i = 1; i < count+1; i++ ) {
                ItemProfile itemProfile = listprofile.get(i-1);


                linearlist = (LinearLayout)findViewById(R.id.layoutlist);
                View[] myView = new View[i];

                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                myView[i-1] = inflater.inflate(R.layout.profile_list, null);

                txtProfileName = (TextView)myView[i-1].findViewById(R.id.txtName);
                txtProfileDate = (TextView)myView[i-1].findViewById(R.id.txtDate);
                txtProfileTime = (TextView)myView[i-1].findViewById(R.id.txtTime);
                txtProfileLocation = (TextView)myView[i-1].findViewById(R.id.txtLocation);

                btnEdit = new FancyButton[i];
                btnEdit = new FancyButton[i];
                btnEdit = new FancyButton[i];


                btnEdit[i-1] = (FancyButton)myView[i-1].findViewById(R.id.btn_edtProfile);
                btnDelete[i-1] = (FancyButton)myView[i-1].findViewById(R.id.btn_deleteProfile);
                btnView[i-1] = (FancyButton)myView[i-1].findViewById(R.id.btn_viewProfile);

                btnEdit[i].setOnClickListener(this);
                btnDelete[i].setOnClickListener(this);
                btnDelete[i].setOnClickListener(this);


                String profileName = itemProfile.getProfileName();
                txtProfileName.setText(profileName);
                Log.d("ProfileName", "onCreate: "+itemProfile.getProfileName());
                int dd = itemProfile.getDay();
                int mm = itemProfile.getMonth();
                int yy = itemProfile.getYear();
                txtProfileDate.setText(dd+"/"+mm+"/"+yy);
                Log.d("Profiledate", "onCreate: "+itemProfile.getDay()+itemProfile.getMonth()+itemProfile.getYear());
                int hour = itemProfile.getHour();
                int min = itemProfile.getMinute();
                txtProfileTime.setText(hour+":"+min);
                Log.d("ProfileTime", "onCreate: "+itemProfile.getHour()+itemProfile.getHour());
                String city = itemProfile.getCity();
                txtProfileLocation.setText(itemProfile.getCity());
                Log.d("citylocation,city", "onCreate: "+itemProfile.getCity()+","+city);
                linearlist.addView(myView[i-1]);


            }


        }


    }

    @Override
    public void onClick(View v) {
        int index = 0;
            for  (int i = 0; i < count; i++)
            {
                if (btnEdit[i].getId() == v.getId())
                {
                    index = i;
                    Toast.makeText(ProfleList.this, "btnEdit"+index, Toast.LENGTH_SHORT).show();
                    break;
                }else
                if (btnDelete[i].getId() == v.getId())
                {
                    index = i;
                    Toast.makeText(ProfleList.this, "btnDelete"+index, Toast.LENGTH_SHORT).show();
                    break;
                }else
                if (btnView[i].getId() == v.getId())
                {
                  index = i;
                    Toast.makeText(ProfleList.this, "btnView"+index, Toast.LENGTH_SHORT).show();
                  break;
                }
            }

            // index is now the array index of the button that was clicked

    }
}

1 个答案:

答案 0 :(得分:2)

改变这个:

 btnEdit = new FancyButton[i];
 btnEdit = new FancyButton[i];
 btnEdit = new FancyButton[i];


 btnEdit[i-1] = (FancyButton)myView[i-1].findViewById(R.id.btn_edtProfile);
 btnDelete[i-1] = (FancyButton)myView[i-1].findViewById(R.id.btn_deleteProfile);
 btnView[i-1] = (FancyButton)myView[i-1].findViewById(R.id.btn_viewProfile);

 btnEdit[i].setOnClickListener(this);
 btnDelete[i].setOnClickListener(this);
 btnDelete[i].setOnClickListener(this);

到那个:

 btnEdit = new FancyButton[i];
 btnDelete= new FancyButton[i];
 btnView= new FancyButton[i];


 btnEdit[i-1] = (FancyButton)myView[i-1].findViewById(R.id.btn_edtProfile);
 btnDelete[i-1] = (FancyButton)myView[i-1].findViewById(R.id.btn_deleteProfile);
 btnView[i-1] = (FancyButton)myView[i-1].findViewById(R.id.btn_viewProfile);

 btnEdit[i].setOnClickListener(this);
 btnDelete[i].setOnClickListener(this);
 btnView[i].setOnClickListener(this);