J'essaie de collecter des entrées utilisateur dans une vue de texte d'édition et un spinner et enregistrer les valeurs dans le stockage interne, puis passer à une autre vue, mais mon activité se bloque après avoir cliqué sur mon bouton de sauvegarde, j'apprécierais que quelqu'un puisse regarder mes codes et me dire ce que je fais de mal.Comment enregistrer spinner et éditer du texte sur le stockage interne et le passer à une autre activité
Mes coordonnées utilisateur d'activité sauver
public class PasswordKeeperActivity extends Activity implements AdapterView.OnItemSelectedListener {
// initialise
EditText username, password, note;
Button save, reset;
public String savedata = Environment.getExternalStorageDirectory().toString();
String[] countryNames={"Google", "Yahoo", "Facebook", "Twitter", "Instagram", "BBM", "Skype", "Other"};
int flags[] = {R.drawable.google, R.drawable.yahoo, R.drawable.facebook, R.drawable.twitter, R.drawable.instagram, R.drawable.bbm, R.drawable.skype, R.drawable.other};
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
// for inflating the menu
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
// on selection of the menu
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.view_passwords:
Intent intent = new Intent(this, PasswordView.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
initialise();
//Getting the instance of Spinner and applying OnItemSelectedListener on
it
Spinner spin = (Spinner) findViewById(R.id.planets_spinner);
spin.setOnItemSelectedListener(this);
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(),
flags, countryNames);
spin.setAdapter(customAdapter);
//to set the site Edit Text to get the focus
// save the data to the textfile
save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// creates hidden directory if not existing
File dir = new File(getCacheDir(), "/sk/");
if (!dir.exists()) {
dir.mkdirs();
}
// saving data part
String sFileName = getCacheDir() + "/sk/logp.csv";
try {
FileWriter writer = new FileWriter(sFileName, true);
String countryNames, sUser, sPass, sAdd;
countryNames =
sUser = username.getText().toString();
sPass = password.getText().toString();
sAdd = note.getText().toString();
if ((sUser.equals("")) && (sPass.equals("")) &&
(sAdd.equals(""))) {
Toast.makeText(getBaseContext(), "Please Enter At least
one Field",
Toast.LENGTH_SHORT).show();
} else {
if (sUser.equals(""))
sUser = "null";
if (sPass.equals(""))
sPass = "null";
if (sAdd.equals(""))
sAdd = "null";
// encrypting the passwords before saving
SimpleCrypto mcrypt = new SimpleCrypto();
sPass = SimpleCrypto.bytesToHex(mcrypt.encrypt(sPass));
//sPass = SimpleCrypto.encrypt("fugly", sPass);
writer.append(sUser);
writer.append(',');
writer.append(sPass);
writer.append(',');
writer.append(sAdd);
writer.append('\n');
// generate whatever data you want
writer.flush();
writer.close();
Toast.makeText(getBaseContext(), "Password Saved!",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(PasswordKeeperActivity.this,
PasswordView.class);
String[] myStrings = new String[] {"Google", "Yahoo",
"Facebook", "Twitter", "Instagram", "BBM", "Skype", "Other"};
int logo[] = new int[] {R.drawable.google,
R.drawable.yahoo, R.drawable.facebook, R.drawable.twitter,
R.drawable.instagram,
R.drawable.bbm, R.drawable.skype, R.drawable.other};
intent.putExtra("strings", myStrings);
intent.putExtra("logos", logo);
startActivity(intent);
}
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
// Reset
reset.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
countryNames.equals("Google");
note.setText("");
username.setText("");
password.setText("");
Toast.makeText(getBaseContext(), "Field(s) Cleared!",
Toast.LENGTH_SHORT).show();
}
});
}
public void initialise() {
username = (EditText) findViewById(R.id.input_name);
password = (EditText) findViewById(R.id.input_email);
note = (EditText) findViewById(R.id.input_password);
save = (Button) findViewById(R.id.buttonSave);
reset = (Button) findViewById(R.id.ButtonReset);
}
/**
* Checks if the app has permission to write to device storage
*
* If the app does not has permission then the user will be prompted to
grant permissions
*
* @param activity
*/
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
}
mon journal
08-15 10:02:44.353 5961-5961/com.com.dreacot.dreacot.fingerprint E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.com.dreacot.dreacot.fingerprint, PID: 5961
java.lang.NullPointerException: Attempt to read from null array
at com.dreacot.fortpasswordkeeper.CustomListAdapter.getView(CustomListAdapter.java:35)
at android.widget.AbsListView.obtainView(AbsListView.java:2929)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1305)
at android.widget.ListView.onMeasure(ListView.java:1212)
at android.view.View.measure(View.java:20151)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:716)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:462)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:714)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(HeaderScrollingViewBehavior.java:90)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(AppBarLayout.java:1391)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:784)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:20151)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6330)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158)
at android.view.View.measure(View.java:20151)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2594)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1549)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1841)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
at android.view.Choreographer.doCallbacks(Choreographer.java:695)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
alors mon adaptateur liste personnalisée
public class CustomListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] countryNames;
private final int flags[];
public CustomListAdapter(Activity context, String[] countryNames, int flags[]) {
super(context, R.layout.mylist, countryNames);
// TODO Auto-generated constructor stub
this.context=context;
this.countryNames=countryNames;
this.flags=flags;
}
public View getView(int position,View view,ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.mylist, null,true);
TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
TextView extratxt = (TextView) rowView.findViewById(R.id.textView1);
txtTitle.setText(countryNames[position]);
imageView.setImageResource(flags[position]);
extratxt.setText("Description "+countryNames[position]);
return rowView;
}
}
modifier
la vue du mot de passe
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpass);
Intent intent = getIntent();
final String[] myStrings = intent.getStringArrayExtra("strings");
int logo[] = intent.getIntArrayExtra("logo");
CustomListAdapter adapter=new CustomListAdapter(this, myStrings, logo);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String Slecteditem= myStrings[+position];
Toast.makeText(getApplicationContext(), Slecteditem, Toast.LENGTH_SHORT).show();
}
});
}
}
Pouvez-vous copier le code où vous instancier la classe CustomListAdapter et comment vous obtenez les params de l'intention? – smora
J'ai édité ma question –
Tentative de lecture à partir d'un tableau vide (CustomListAdapter.java:35), cela concerne-t-il le tableau "countryNames" ou "flags"? – smora