
[英]App works fine when running from Android Studio but crashes when signed apk installed
[英]Android app is working only when I installed it from Android Studio But not when I install the APK directly on other phones
我是Android开发的新手,正在尝试使用sharedprefernces保存字符串。 当我将手机用作仿真器时,一切正常。 Android Studio在我的手机上安装了该应用程序,并且可以正常运行。
问题是,我使用shareIt将apk文件传输到其他手机,但无法正常工作。 Toast正在运行,但是当我单击“保存”按钮时,应用程序关闭,但不幸地停止了工作。
然后,我使用Android Studio安装了相同的项目,并且该项目正常工作。
我在3种设备上进行了测试,所有问题都相同。 直接使用APK安装时为何不起作用。
这是代码。
MainActivity.java
package geekdashboard.learningtetsing;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public static final String PREFS_NAME = "PROFILE_CHANGER_KEYWORDS";
public static final String PREFS_KEY = "silent";
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void toastTesting(View view) {
// Code for Toast
Toast.makeText(MainActivity.this,
"Toast is Working :)", Toast.LENGTH_SHORT).show();
// Toast End
}
public void savingValue(View v) {
// Reading value from name editField
EditText nameField = (EditText) findViewById(R.id.name);
String enteredName = nameField.getText().toString();
if (enteredName.equals("")) {
Toast.makeText(MainActivity.this,
"Empty Keyword Not Allowed", Toast.LENGTH_SHORT).show();
} else {
SharedPreferences prefs = this.getSharedPreferences(
"com.geekdashboard.learningtetsing", Context.MODE_PRIVATE);
prefs.edit().putString(PREFS_KEY, enteredName).commit();
Toast.makeText(MainActivity.this,
enteredName + " has been saved to " + PREFS_NAME, Toast.LENGTH_SHORT).show();
}
}
public void showValue(View sv) {
SharedPreferences prefsread = this.getSharedPreferences(
"com.geekdashboard.learningtetsing", Context.MODE_PRIVATE);
String savedValue = prefsread.getString(PREFS_KEY, null);
Toast.makeText(MainActivity.this,
savedValue + " is the last saved value in "+ PREFS_NAME, Toast.LENGTH_SHORT).show();
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<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="geekdashboard.learningtetsing.MainActivity">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/buttonString"
android:onClick="toastTesting"
android:layout_marginTop="10dp"
android:background="#212121"
android:textColor="#FFFFFF"
android:id="@+id/button" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/buttonShared"
android:background="@color/accent_material_light"
android:textColor="#FFFFFF"
android:id="@+id/save"
android:onClick="savingValue"
android:layout_below="@+id/name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="Enter Your Name Here"
android:ems="10"
android:id="@+id/name"
android:singleLine="true"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="43dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/showValue"
android:id="@+id/show"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:background="@android:color/holo_blue_dark"
android:textColor="#FFFFFF"
android:onClick="showValue"/>
</RelativeLayout>
表现
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="geekdashboard.learningtetsing">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
手动安装apk文件时,showValue和saveValue方法不起作用。
谢谢
我更改了使用sharedPreferences的方式,并且有效。 这是修改后的代码。
package geekdashboard.learningtetsing;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public static final String PREFS_NAME = "PROFILE_CHANGER_KEYWORDS";
public static final String PREFS_KEY = "silent";
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void toastTesting(View v) {
// Code for Toast
Toast.makeText(MainActivity.this,
"Toast is Working :)", Toast.LENGTH_SHORT).show();
// Toast End
}
public void savingValue(View view) {
// Reading value from name editField
EditText nameField = (EditText) findViewById(R.id.name);
String enteredName = nameField.getText().toString();
if (enteredName.equals("")) {
Toast.makeText(MainActivity.this,
"Empty Keyword Not Allowed", Toast.LENGTH_SHORT).show();
} else {
// Context context = getActivity();
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
//prefs.edit().putString(PREFS_KEY, enteredName).commit();
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(PREFS_KEY, enteredName);
editor.commit();
Toast.makeText(MainActivity.this,
enteredName + " has been saved to " + PREFS_NAME, Toast.LENGTH_SHORT).show();
}
}
public void showValue(View sv) {
SharedPreferences sharedPref = this.getPreferences(Context.MODE_PRIVATE);
String defaultValue = "Silent";
String savedValue = sharedPref.getString(PREFS_KEY, defaultValue);
/* SharedPreferences prefsread = this.getSharedPreferences(
"com.geekdashboard.learningtetsing", Context.MODE_PRIVATE);
String savedValue = prefsread.getString(PREFS_KEY, null); */
Toast.makeText(MainActivity.this,
savedValue + " is the last saved value in "+ PREFS_NAME, Toast.LENGTH_SHORT).show();
}
}
是ProGuard配置错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.