2010-11-15 82 views
0

我似乎在向越來越應用程序崩潰:Android的崩潰使用setVisibility(View.VISIBLE)時

parent.findViewById(R.id.a_1).setVisibility(View.VISIBLE);

不知道什麼即時做錯了

代碼如下

layouttext.java

public class layouttest extends Activity { 
private Spinner solvefor; 
private ArrayAdapter<CharSequence> featuresAdapter; 
private List<CharSequence> featuresList; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     featuresList = new ArrayList<CharSequence>(); 
     featuresAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, featuresList); 
     featuresAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     solvefor = ((Spinner) layouttest.this.findViewById(R.id.solvefor)); 
     solvefor.setAdapter(featuresAdapter); 
     featuresAdapter.add("Velocity"); 
     featuresAdapter.add("Time"); 
     featuresAdapter.add("Distance"); 
     solvefor.setOnItemSelectedListener(new MyOnItemSelectedListener()); 
    } 
} 

MyOnItemSelectedListener.java 包android.example.layouttest;

import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.Toast; 

public class MyOnItemSelectedListener implements OnItemSelectedListener { 
private static final int SPINNER_OPTION_FIRST = 0; 
    private static final int SPINNER_OPTION_SECOND = 1; 
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { 
    switch(pos) { 
     case SPINNER_OPTION_FIRST: { 
      Toast.makeText(parent.getContext(), "0", Toast.LENGTH_LONG).show(); 
      break; 
     } 
     case SPINNER_OPTION_SECOND: { 
      Toast.makeText(parent.getContext(), "1", Toast.LENGTH_LONG).show(); 
      parent.findViewById(R.id.a_1).setVisibility(View.VISIBLE); 
      parent.findViewById(R.id.a_2).setVisibility(View.VISIBLE); 
      break; 
     } 
    } 
} 
public void onNothingSelected(AdapterView<?> parent) {  
    // Do nothing.  
    } 
} 
+2

你確定它是'parent'你想要查找view而不是'view'嗎? – Falmarri 2010-11-15 23:57:23

+2

您可以添加應用程序日誌和異常堆棧跟蹤。沒有他們,很難提供幫助。 – Juhani 2010-11-16 00:16:53

回答

0

記住View#findViewById開始從你調用它,並且下降到其子視圖其搜索。如果您要查找的ID不是視圖本身,也不是該視圖的後代,則搜索將失敗,findViewById將返回null

正如Falmarri所建議的那樣,您可能不想在parent的電話號碼findViewById上。 (您可能也不想在view之後從那裏調用它。)

在您的視圖層次結構中,您希望找到R.id.a_1

相關問題