我正在玩Android Studio制作一个非常简单的非常愚蠢的应用来学习保存关键偏好,我遇到了一个奇怪的障碍。我会尝试尽可能多地提供,因为它可能很难重现这个错误但老实说,我正在运行的两个应用程序都是超级基本并且没有编译错误
规格: 没有模拟器,我正在运行三星Galaxy平板电脑。 Windows 7,Android Studio 1.2,Gradle 2.2.1。
在问题标题中,我的意思是我有一个名为 Kitty 的项目(几乎是hello world和一个按钮)。我点击运行 - >'运行应用' - >(对话框打开) - > OK->应用程序在我的平板电脑上启动。
^^^这是美丽的屏幕,我想看到共享偏好,但它只在 kitty 上。
现在我开始了另一个名为 SharedPreferences 的项目(要点:两个复选框要求你“你喜欢巧克力”和“你喜欢luigi”而你检查一个或两个都没有,然后按保存。两个文本视图在下面会更新,如果你喜欢这些东西,甚至以后如果你重新打开应用程序,textviews会记住Chocolate Luigi首选项)。这只是一个主要的活动。
我认为我没有更改两者之间的任何设置或项目偏好,也没有给我一个错误。 MainActivity.java 外观原始编辑:
06-02 20:49:57.245 25557-25557/? I/SDP.PUB_CRYPTOD﹕ Starting
06-02 20:49:57.245 25557-25557/? I/SDP.PUB_CRYPTOD﹕ Socket created with fd:-1
06-02 20:49:57.245 25557-25557/? E/SDP.PUB_CRYPTOD﹕ Failed to open the netlink socket with error: Protocol not supported
06-02 20:49:57.245 25557-25557/? E/SDP.PUB_CRYPTOD﹕ Exiting
06-02 20:49:59.995 2866-3012/? V/AlarmManager﹕ waitForAlarm result :8
06-02 20:50:02.280 25633-25633/? I/SDP.PUB_CRYPTOD﹕ Starting
06-02 20:50:02.280 25633-25633/? I/SDP.PUB_CRYPTOD﹕ Socket created with fd:-1
06-02 20:50:02.280 25633-25633/? E/SDP.PUB_CRYPTOD﹕ Failed to open the netlink socket with error: Protocol not supported
06-02 20:50:02.280 25633-25633/? E/SDP.PUB_CRYPTOD﹕ Exiting
logcat的红色部分:
package gaga.sharedpreferences;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CheckedTextView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
public final class setup extends MainActivity {
public void setup () {
//Nothing to see here!
}
// Define the File of Prefs; created if nonexistent
public static final String PREFS_NAME = "MyPrefsFile";
// Start up
public void onCreateSubclass() {
// super.onCreate(state);
// Restore preferences on Startup
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean Chocolate = settings.getBoolean("checkChocolate", false);
boolean Luigi = settings.getBoolean("checkLuigi", false);
// Function set to Whatever
// setSilent(silent);
/* Note:
* CheckedTextView and CheckBox::isChecked()
* CheckBox::setChecked()
* */
CheckBox checkHandleChocolate = (CheckBox) findViewById(R.id.checkChocolate);
CheckBox checkHandleLuigi = (CheckBox) findViewById(R.id.checkLuigi);
// What was the preference? On Start set it to the bool it left off in
checkHandleChocolate.setChecked(Chocolate);
checkHandleLuigi.setChecked(Luigi);
// Change report text on Start
TextView buttonHandleChocolate = (TextView) findViewById(R.id.chocolate);
TextView buttonHandleLuigi = (TextView) findViewById(R.id.luigi);
if(Chocolate)
buttonHandleChocolate.setText("I do prefer Chocolate");
else
buttonHandleChocolate.setText("I do not prefer Chocolate");
if(Luigi)
buttonHandleLuigi.setText("I do prefer Luigi");
else
buttonHandleLuigi.setText("I do not prefer Luigi");
}
public void saveChocolate(Boolean c) {
// All objects from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("Chocolate", c);
// Commit the edits
editor.commit();
}
public void saveLuigi(Boolean l) {
// All objects from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("Chocolate", l);
// Commit the edits
editor.commit();
}
}
@Override
protected void onStop(){
super.onStop();
// Objects are from android.context.Context
//Normally I'd put the edit commits here, but that's not true
}
// Clicks on Done
public void userDone (View view) {
// View is which widget
boolean checked = ((CheckBox) view).isChecked();
// Which checkbox was clicked
switch(view.getId()) {
case R.id.checkChocolate:
setup instance1 = new setup();
instance1.saveChocolate(checked);
// No break; continue along
case R.id.checkLuigi:
setup instance2 = new setup();
instance2.saveLuigi(checked);
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setup startInstance = new setup();
startInstance.onCreateSubclass();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
感谢您的帮助。我在浏览互联网时没有看到这个问题所以它可能是过分的菜鸟。
编辑:用较大的MainActivity类中唯一的onCreate重写
{{1}}
答案 0 :(得分:1)
您似乎有两个onCreate方法。尝试删除第二个并再次运行它。
答案 1 :(得分:1)
在Android Studio中,您需要在项目中创建一个运行配置。
转到左侧运行图标
的链接
然后点击编辑配置,在Windows配置后如下:
并保存,用于测试点击运行图标。