如果存在,则在Onpause()中的共享preferences访问 - 应用程序崩溃
我正在进行一个学校项目,因此,具体来说,需要从Onpause方法中保存共享的偏好,然后在onCreate方法中调用,并在EditText的值中放置在EssharePreferences中的价值中。活动中的两个“ button_dont”中的按钮应该只带您回到活动中,但是现在我添加了它不断关闭应用程序的Onpause方法。
最重要的是,我不明白如何从使用的仿真器中访问ADB。我尝试从Android Studio中找到DeviceManager中的文件,但在数据文件中找不到我的项目名称。我是新手。接下来我可以尝试什么?
<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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:layout_row="4"
android:layout_column="3"
android:layout_weight="1"
android:layout_gravity="fill"
>
<TextView
android:id="@+id/textView"
android:layout_width="256dp"
android:layout_height="66dp"
android:layout_row="0"
android:layout_column="0"
android:text="@string/enterName"
android:textSize="30sp" />
<EditText
android:id="@+id/editText"
android:layout_width="441dp"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:layout_columnSpan="2"
android:textSize="40sp"/>
<Button
android:id="@+id/next"
android:layout_width="127dp"
android:layout_height="64dp"
android:layout_row="2"
android:layout_column="0"
android:text="@string/next" />
</GridLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NameActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/welcome"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="416dp"
android:layout_height="87dp"
android:orientation="horizontal">
<Button
android:id="@+id/button_dont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dont_call"
android:layout_margin="5sp"
/>
<Button
android:id="@+id/button_do"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/thank_you"
android:layout_margin="5sp"/>
</LinearLayout>
</LinearLayout>
代码:
public class NameActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
TextView welcome = findViewById(R.id.welcome);
Intent i = getIntent();
String name = i.getStringExtra("Value");
welcome.setText("Welcome "+ name +"!");
Button button_dont = findViewById(R.id.button_dont);
Button button_do = findViewById(R.id.button_do);
button_dont.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent();
i.putExtra("result",0);
setResult(Activity.RESULT_OK,i);
finish();
}
});
button_do.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent();
i.putExtra("result",1);
setResult(RESULT_OK, i);
finish();
}
});
}
}
public class MainActivity extends AppCompatActivity {
Button next;
EditText editText;
public static final int REQ_CODE=1014;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = findViewById(R.id.next);
EditText editText = findViewById(R.id.editText);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),NameActivity.class);
String name = editText.getText().toString();
i.putExtra("Value",name);
startActivityForResult(i,1014);
}
});
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences sharedPrefs = getSharedPreferences("Names", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString("name", editText.getText().toString());
editor.commit();
}
@Override
public void onActivityResult (int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
int result = data.getIntExtra("result", 2);
if (result == 1) {
finish();
}
}}
I am doing a school project, so specifically, the shared preferences needs to be saved from the OnPause method, then called in the OnCreate method and placed into the value of the editText if it exists in sharedPreferences. The button in activity two "button_dont" should just take you back to activity one, which it does, but now I have added the onPause method it keeps closing the app.
On top of this, I don't understand how to access the ADB from the emulator I am using. I tried finding the file in DeviceManager from android studio but I can't find my project name within the data file. I'm new at this. What can I try next?
<?xml version="1.0" encoding="utf-8"?>
<GridLayout 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:layout_row="4"
android:layout_column="3"
android:layout_weight="1"
android:layout_gravity="fill"
>
<TextView
android:id="@+id/textView"
android:layout_width="256dp"
android:layout_height="66dp"
android:layout_row="0"
android:layout_column="0"
android:text="@string/enterName"
android:textSize="30sp" />
<EditText
android:id="@+id/editText"
android:layout_width="441dp"
android:layout_height="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:layout_columnSpan="2"
android:textSize="40sp"/>
<Button
android:id="@+id/next"
android:layout_width="127dp"
android:layout_height="64dp"
android:layout_row="2"
android:layout_column="0"
android:text="@string/next" />
</GridLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".NameActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/welcome"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:layout_width="416dp"
android:layout_height="87dp"
android:orientation="horizontal">
<Button
android:id="@+id/button_dont"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dont_call"
android:layout_margin="5sp"
/>
<Button
android:id="@+id/button_do"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/thank_you"
android:layout_margin="5sp"/>
</LinearLayout>
</LinearLayout>
The code:
public class NameActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
TextView welcome = findViewById(R.id.welcome);
Intent i = getIntent();
String name = i.getStringExtra("Value");
welcome.setText("Welcome "+ name +"!");
Button button_dont = findViewById(R.id.button_dont);
Button button_do = findViewById(R.id.button_do);
button_dont.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent();
i.putExtra("result",0);
setResult(Activity.RESULT_OK,i);
finish();
}
});
button_do.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent();
i.putExtra("result",1);
setResult(RESULT_OK, i);
finish();
}
});
}
}
public class MainActivity extends AppCompatActivity {
Button next;
EditText editText;
public static final int REQ_CODE=1014;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button next = findViewById(R.id.next);
EditText editText = findViewById(R.id.editText);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),NameActivity.class);
String name = editText.getText().toString();
i.putExtra("Value",name);
startActivityForResult(i,1014);
}
});
}
@Override
protected void onPause() {
super.onPause();
SharedPreferences sharedPrefs = getSharedPreferences("Names", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString("name", editText.getText().toString());
editor.commit();
}
@Override
public void onActivityResult (int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
int result = data.getIntExtra("result", 2);
if (result == 1) {
finish();
}
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能在Onpause中创建并在OnCreate上访问,只能在创建和初始化onCreate的外部创建,然后在其他功能中使用。
为此,您需要在ongreate中初始化该功能,然后您可以在其他地方使用。
像这样
You cannot create in onpause and access on oncreate, you can only create outside on create and initialize in oncreate then use in other functions.
For this, u need to initialize the function in oncreate, then u can use else where.
Like this