我在玩Android Studio,制作一个非常简单,非常愚蠢的应用,以了解 有关保存关键首选项的知识,但遇到了一个奇怪的障碍。我将尽力 提供尽可能多的内容,因为可能很难重现此错误,但是 老实说,我正在运行的两个应用程序都是超级基本的,并且没有编译 错误。
规格:没有模拟器,我正在运行Samsung Galaxy Tablet。Windows 7,Android Studio 1.2,Gradle 2.2.1。
在问题标题中,我的意思是我有一个名为Kitty的项目(几乎是hello world和一个按钮)。我单击运行->“运行应用程序”->( 打开对话框)->确定->,然后立即在平板电脑上启动该应用程序。
这是我要看的盒子!
^^^这是我想在Sharedpreferences上看到的美丽屏幕,但这仅是在kitty上。
现在,我启动了另一个名为SharedPreferences的项目(要点:两个复选框询问您“您喜欢巧克力”还是“您喜欢路易斯饼干”,然后选择一个或两个都不选中,然后按Save。下面的两个textview将更新以表示您是否 喜欢这些东西)甚至在以后重新打开应用程序时,textviews也会记住Chocolate Luigi首选项。这只是一个main_activity。
我不认为我在这两者之间更改了任何设置或项目首选项,也没有给我一个错误。MainActivity.java过时的原始编辑:
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 @Override public void onCreate(Bundle state) { 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); } @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); } }
Red parts of logcat:
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
Thanks for any help. I haven’t seen this issue while prowling the internet so it might be excessively noob.
EDIT: Rewritten with the only onCreate in the larger MainActivity class
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); } }
在Android Studio中,您需要在项目中创建运行配置。