【问题标题】:Return Button - unreachable返回按钮 - 无法访问
【发布时间】:2013-04-07 11:59:45
【问题描述】:

我想创建一个关闭当前活动的按钮。就像一个“返回”按钮。 以下是我尝试过的代码片段:

这是完整的.java:

public class OtherApps extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.other_apps);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.otherappsmenu, menu);
        return true; 
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {  
        case R.id.previous:
            finish();
            break;
        case R.id.home:
            Context context = getApplicationContext();
            CharSequence text = "Activitys are not closed!";
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            Intent intent = new Intent(this, MainActivity.class);
            this.startActivity(intent);
            break;
        case R.id.exit:
            finish();
            System.exit(0);
        case R.id.help:
            String url = "http://www.google.de/";
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
        return true;

        final Button OtherApps = (Button)findViewById(R.id.previousbutton);
        OtherApps.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
            }
        });
        return true;
    }
}

但是 Eclipse 说“第一行无法访问”。 有谁知道错误是什么?

感谢您的帮助!

【问题讨论】:

  • “第一行”——究竟是哪一行?
  • 哪个代码无法访问?
  • 每个代码的第一行。 "按钮按钮 = (Button) findViewById(R.id.previousbutton);"和“ final Button OtherApps = (Button)findViewById(R.id.previousbutton);”
  • 您尝试在onOptionsItemSelected 底部添加previousButton 处理程序,在switch 块和return 之后。尝试将其移至 onCreate 方法。
  • @YouDeveloper 我已经为您修复了代码缩进。现在问题应该很明显了。

标签: android button android-activity


【解决方案1】:

如果您的“第一行”无法访问,那么更重要的是要知道这两个版本之前的代码是什么。可能是,您有一个 return 语句或一个始终为 false 的条件。

在这种情况下,将永远无法访问附加点击侦听器的代码。

附言

你有两行从方法返回,这意味着下面的代码永远不会执行:

switch(item.getItemId()) {  
   ...
   default: // here, return if none of the values above matched
     return super.onOptionsItemSelected(item); 
}
return true; // here, return always

// conclusion: this gets never executed: Eclipse says "line not reachable"
final Button OtherApps = (Button ...

【讨论】:

    【解决方案2】:

    这段代码应该可以工作(应该首选第一个示例)。

    在粘贴代码之前,您收到的错误听起来好像您在该方法中的任何位置都有return-statement。搜索那个,它应该可以修复错误。

    编辑:

    public class OtherApps extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.other_apps);
    
            final Button OtherApps = (Button) findViewById(R.id.previousbutton);
            OtherApps.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
                }
            });
    }
    

    【讨论】:

    • 你的意思是返回true; ?我有 3 个。
    • 正如 Deanna 评论的那样,请在这些行之前发布代码。问题将出在return trues 之一!
    • 我无法回答自己的问题,但这里是 Google Docs 上的代码:docs.google.com/document/d/…
    • @YouDeveloper 您可以编辑您的帖子以添加它。答案应该只为答案而发布。 (StackOverflow 是一个问答网站,不是论坛)
    • 好的,.java 在我的第一篇文章中。
    猜你喜欢
    • 1970-01-01
    • 2018-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    • 2023-02-12
    • 2011-12-10
    相关资源
    最近更新 更多