如果存在,则在Onpause()中的共享preferences访问 - 应用程序崩溃

发布于 2025-02-01 20:31:42 字数 5766 浏览 2 评论 0原文

我正在进行一个学校项目,因此,具体来说,需要从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

短暂陪伴 2025-02-08 20:31:42

您不能在Onpause中创建并在OnCreate上访问,只能在创建和初始化onCreate的外部创建,然后在其他功能中使用。

为此,您需要在ongreate中初始化该功能,然后您可以在其他地方使用。
像这样

public class MainActivity extends AppCompatActivity {
    Button next;
    EditText editText;
    public static final int REQ_CODE=1014;
    SharedPreferences sharedPrefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 sharedPrefs = getSharedPreferences("Names", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPrefs.edit();
        editor.putString("name", editText.getText().toString());
        editor.commit();

        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();
       
    }

    @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();
        }
    }}````

So when u do it like this, u can call and use it anywhere in the class

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

public class MainActivity extends AppCompatActivity {
    Button next;
    EditText editText;
    public static final int REQ_CODE=1014;
    SharedPreferences sharedPrefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 sharedPrefs = getSharedPreferences("Names", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPrefs.edit();
        editor.putString("name", editText.getText().toString());
        editor.commit();

        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();
       
    }

    @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();
        }
    }}````

So when u do it like this, u can call and use it anywhere in the class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文