我在添加应用用户数据时遇到问题。该应用程序无法运行。 [英] I have a problem adding my apps user data. The application does not run.

查看:74
本文介绍了我在添加应用用户数据时遇到问题。该应用程序无法运行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个程序,它接受来自edittext的输入并使用Sharedpreferences存储它,当我按下retrive按钮时,它必须检索首先从编辑文本中获取的字符串。但该程序根本没有运行。



这是XML代码





< relativelayout xmlns:android =http://schemas.android.com/apk/res/android>

xmlns:tools =http://schemas.android .com / tools

android:layout_width =match_parent

android:layout_height =match_parent

android:paddingBottom =@ dimen / activity_vertical_margin

android:paddingLeft =@ dimen / activity_horizo​​ntal_margin

android:paddingRight =@ dimen / activity_horizo​​ntal_margin

android:paddingTop =@ dimen / activity_vertical_margin

工具:context =app.com.example.android.myapplication.MainActivity>



< edittext>

android:layout_width =match_parent

android:layout_height =wrap_content

android:hint =输入内容

android:id =@ + id / editText/>


<按钮

android:layout_width =wrap_content

android:layout_height =wrap_content

android: text =CLICK ME

android:id =@ + id / button

android:/>



<按钮

android:layout_width =wrap_content

android:layout_height =wrap_content

android:text =回复你写的

android:/>







java代码



包app.com.example.android.myapplication;



import android.app .Activity;

import android.content.Context;

import android.content.SharedPreferences;

import android.support.v7.app。 AppCompatActivity;

导入android.os.Bundle;

导入android.util.Log;

导入android.view.View;

导入android.widget.Button;

impo rt android.widget.EditText;

导入android.widget.TextView;

导入android.widget.Toast;



公共类MainActivity扩展AppCompatActivity {



Button clickme;

EditText typeme;

String New;

String Next =Nothing;

SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);



@覆盖

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main); < br $>


clickme =(按钮)findViewById(R.id.button);

typeme =(EditText)findViewById(R.id.editText) ;



}



public void dosomething(查看视图){



String textsomething = typeme.getText()。toString();

Toast.makeText(this,texts omething +WORKS OK,Toast.LENGTH_LONG)。show();



SharedPreferences.Editor editor = sharedPref.edit();

。Log.i(MyActivity,editting opened);

editor.putString(New,textsomething);



编辑器。 commit();

Log.i(MyActivity,编辑结束);









}





public void getsomething(查看视图){



Next = sharedPref.getString(New,Next).toString();

Log.i(MyActivity,Retrieving started );

Toast.makeText(this,Next +Retrived value Hurray,Toast.LENGTH_LONG)。show();

Log.i(MyActivity,检索完成华友世纪的作品);



}

}





有些人请帮助我新手。提前致谢



我尝试过:



搜索它谷歌,但不能。

I have created a program that takes in a input from edittext and stores it using Sharedpreferences and when i press retrive button it must retrive the string what was first taken from edit text. But the program does not run at all.

This is the XML Code


<relativelayout xmlns:android="http://schemas.android.com/apk/res/android">
xmlns:tools="http://schemas.android.com/tools"
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"
tools:context="app.com.example.android.myapplication.MainActivity">

<edittext>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type something"
android:id="@+id/editText" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CLICK ME"
android:id="@+id/button"
android:/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Retrive what you wrote"
android:/>



The java code

package app.com.example.android.myapplication;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

Button clickme;
EditText typeme;
String New;
String Next = "Nothing";
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);

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

clickme = (Button) findViewById(R.id.button);
typeme = (EditText) findViewById(R.id.editText);

}

public void dosomething(View view) {

String textsomething = typeme.getText().toString();
Toast.makeText(this, textsomething + "WORKS OK", Toast.LENGTH_LONG).show();

SharedPreferences.Editor editor = sharedPref.edit();
Log.i("MyActivity", "editting opened");
editor.putString(New, textsomething);

editor.commit();
Log.i("MyActivity", "editing over");




}


public void getsomething(View view) {

Next = sharedPref.getString(New, Next).toString();
Log.i("MyActivity", "Retrieving started");
Toast.makeText(this, Next + "Retrived value Hurray", Toast.LENGTH_LONG).show();
Log.i("MyActivity", "Retrieving done Hurray works");

}
}


Some please help as im newbie. Thanks in advance

What I have tried:

searching for it google but could not.

推荐答案

您的代码中没有任何内容可以调用方法来完成实际工作。我建议你阅读在Android上处理输入和存储 [ ^ ],它解释了这样做。
There is nothing in your code to call the methods to do the actual work. I suggest you read Handling Input and Storage on Android[^], which explains how to do it.


这篇关于我在添加应用用户数据时遇到问题。该应用程序无法运行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆