Java 安卓工作室';运行应用程序&x27;对话不';不会出现在一个项目中,但会出现在另一个项目中

Java 安卓工作室';运行应用程序&x27;对话不';不会出现在一个项目中,但会出现在另一个项目中,java,android,android-studio,Java,Android,Android Studio,我正在玩弄Android Studio制作一个非常简单非常愚蠢的应用程序来学习如何保存键首选项,我遇到了一个奇怪的障碍。我会尽可能多地提供,因为可能很难重现这个bug,但老实说,我运行的两个应用都是超级基本,并且没有编译错误 规格: 没有模拟器,我正在运行三星Galaxy平板电脑。Windows7、Android Studio 1.2、Gradle 2.2.1 在问题标题中,我的意思是我有一个名为Kitty的项目(相当于hello world和一个按钮)。我点击Run->“Run app”->(

我正在玩弄Android Studio制作一个非常简单非常愚蠢的应用程序来学习如何保存键首选项,我遇到了一个奇怪的障碍。我会尽可能多地提供,因为可能很难重现这个bug,但老实说,我运行的两个应用都是超级基本,并且没有编译错误

规格: 没有模拟器,我正在运行三星Galaxy平板电脑。Windows7、Android Studio 1.2、Gradle 2.2.1

在问题标题中,我的意思是我有一个名为Kitty的项目(相当于hello world和一个按钮)。我点击Run->“Run app”->(对话框打开)->OK->应用程序在我的平板电脑上启动

^^^这是我想在SharedReferences上看到的漂亮屏幕,但只在kitty上

现在,我启动了另一个名为SharedReferences的项目(要点:两个复选框问你“你喜欢巧克力吗”和“你喜欢路易吉吗”,你选中一个“不喜欢”或两个复选框,然后按save。下面的两个文本视图将更新,说明你是否喜欢这些东西,甚至在以后重新打开应用程序时,文本视图将记住巧克力路易吉的首选项)。这只是一项主要活动

我不认为我改变了两者之间的任何设置或项目首选项,也没有给我一个错误。MainActivity.java过时的原始版本请参见编辑:

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
谢谢你的帮助。我在网上闲逛时没有看到过这个问题,所以它可能是过度的noob

编辑:在较大的MainActivity类中仅使用onCreate重写

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

看起来您有两个onCreate方法。尝试删除第二个并再次运行。

在Android Studio中,您需要在项目中创建运行配置

转到此链接至左跑步图标

然后单击编辑配置,然后在windows中进行如下配置:


然后保存它,为测试单击运行图标。

看起来您有两个onCreate方法;我认为这会有所不同,因为其中一个是在一个名为setup的子类中,我不知道为什么androidstudio没有捕捉到它on@Santiago抱歉,我更新了它,但显示了相同的内容。logcat上方右侧的选择框仍然会显示“无可调试应用程序”@vkuo OK(出于某种原因),如果我启动一个新项目并移动它突然在新项目上运行的java和xml文件,则会显示“无可调试应用程序”。我有点不高兴,因为现在这个错误会不时出现,我不知道它是什么。经过再三考虑,Santiago刚刚发布了我将尝试它。不幸的是,这并没有解决问题+脚本Kitty不,ConsultActivity是我项目中测试活动的名称,在你的Android Studio项目中,这将是主要活动
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);
    }
}