当前位置: 首页 > 面试题库 >

Android Studio“运行应用”对话框未出现在一个项目中,但将出现在另一个项目中

纪枫
2023-03-14
问题内容

我在玩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中,您需要在项目中创建运行配置。



 类似资料:
  • 我使用MFC和对话框创建了两个独立的可执行文件项目。 我是visual studio的新手,所以请给出一些建议。谢谢

  • 我的firebase函数在开发中运行正常(D),但在生产中给了我以下错误。此外,当我检查firebase functions控制台上的日志时,似乎没有任何活动。就好像函数没有被调用一样。 这里是我的angular应用程序调用函数的代码- 以下是功能- 索引.ts GPublish.ts 在开发和生产中部署功能时,我已经正确地更改了服务帐户密钥和google凭据。 null

  • 在Python中,我有一个ndarray,它打印为 对象没有属性 我该怎么办?

  • 我正在构建一个定制的Java库。我把我的大部分“重复”代码都保存在那里,比如文件处理、字符串处理等。每次我想使用它们时,我都必须将该类复制并粘贴到我正在进行的其他项目中。有没有办法让这个自定义库类成为“依赖项”?我在用我的智能手机。

  • 我在两个项目上使用相同的容器(类似的资源)--生产和阶段。两者都有自定义域与云耀斑DNS设置,并在同一地区。容器构建是在一个完全不同的项目中完成的,IAM用于处理对这些容器的访问。对于所有5个服务,这两个项目服务都有80个并发和300秒超时。 三天前一切都很好,但从昨天开始,几乎所有的云运行服务(谢天谢地)都开始随机抛出503个请求。有些服务甚至一周都没有部署。同样的容器在生产项目上运行良好,没有

  • 我有以下控制器: 我想这意味着一些连接正在工作,但我在网页上得到了一个404错误。 奇怪的是,当我将这两个类复制粘贴到另一个项目(在一个我以前工作过并知道工作的包中)时,代码运行良好。但是,如果在同一个项目中,我创建了一个新的包,并在该包中放置了新的控制器和一个新的启动类,那么控制器也不能工作(404错误)。 我使用eclipse构建了这个项目,并在类路径中包含了所有依赖项,我从来不需要创建web