spinner中无法访问的代码android

时间:2014-01-29 03:11:37

标签: android android-spinner

我正在“尝试”制作一个Android应用程序。我试图让它创建一个

spinner if ... else突然显示“错误消息”Unreachable code

代码:

Spinner localSpinner = (Spinner)findViewById(R.id.spinner);

ArrayAdapter localArrayAdapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, this.arr);

localArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

localSpinner.setAdapter(localArrayAdapter);

localSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()

{

public void onItemSelected(AdapterView<?> parent, View v, int pos, long id)
  {
    if (pos == 0)
    {
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab31)).setVisibility(View.VISIBLE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab32)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab33)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab34)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab35)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab36)).setVisibility(View.GONE);        
    }
    do
    {
      return;
      if (pos == 1)
      {
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab31)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab32)).setVisibility(View.VISIBLE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab33)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab34)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab35)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab36)).setVisibility(View.GONE);
        return;
      }
      if (pos == 2)
      {
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab31)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab32)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab33)).setVisibility(View.VISIBLE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab34)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab35)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab36)).setVisibility(View.GONE);
        return;
      }
      if (pos == 3)
      {
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab31)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab32)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab33)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab34)).setVisibility(View.VISIBLE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab35)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab36)).setVisibility(View.GONE);
        return;
      }
      if (pos == 4)
      {
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab31)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab32)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab33)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab34)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab35)).setVisibility(View.VISIBLE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab36)).setVisibility(View.GONE);
        return;
      }
    }

    while (pos != 5);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab31)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab32)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab33)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab34)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab35)).setVisibility(View.GONE);
      ((LinearLayout)MainKasama.this.findViewById(R.id.tab36)).setVisibility(View.VISIBLE);
  }

  public void onNothingSelected(AdapterView<?> parent) {}
});

我的代码中还有一个问题

代码:

import android.app.AlertDialog.Builder;

final AlertDialog.Builder Builder = new AlertDialog.Builder(this);

有错误消息AlertDialog cannot be resolved to a type如何修复该错误

感谢您回答我的问题

3 个答案:

答案 0 :(得分:2)

do
{
  return;  // remove this return 
  if (pos == 1)

答案 1 :(得分:0)

@Pulkit Sethi的答案是正确的,但他没有提供任何理由说明为什么必须删除return。您可能错误地插入了return,或者您可能不知道return会做什么。我正在考虑后者作为案例,从而提供解释。

每当编译器以任何编程语言获得return时,它立即退出循环并且不执行下面的所有语句。这就是你在spinner 中获得无法访问的代码android的原因。

答案 2 :(得分:0)

在您的活动中,这段代码是: final AlertDialog.Builder Builder = new AlertDialog.Builder(this);

检查它是否在侦听器或其他地方,指定“context = this;”在你的活动的oncreate或onresume

AlertDialog.Builder的“this”可能不是上下文