-1
Ich bekomme Rumpf Array-Fehler, wenn ich versuche, meine Tasten zu initialisieren btnEdit, btnDelete, btnView Ich weiß, das Problem ist mit der Initialisierung, aber ich weiß nicht, wo genau kann jemand helfen? Ich weiß, das Problem ist mit der Initialisierung, aber ich weiß nicht wo genau, kann jemand helfen?java.lang.NullPointerException: Versuch, auf Null-Array zu schreiben
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
}
}
jetzt seine geben mir FancyButton.getId()‘auf einem Null-Objektverweis in der for-Schleife bei IF, wo ich bin Vergleichen Sie die IDs – bipin
In der Tat, bekomme ich nicht, was Sie hier erreichen möchten ... Könnten Sie Ihren Layout-Code hinzufügen? – malrok44
Sie gaben mir richtige Antwort und jetzt funktioniert mein Code, aber ich erhalte einen neuen Fehler, wenn ich auf einen der Button klicke gibt es mir FancyButton.getId() 'auf einen Null-Objektreferenz in der for-Schleife bei IF wo Ich vergleiche die IDs – bipin