我遇到了一个问题:当我尝试在我的设备中调试我的应用时,它会在启动后立即关闭它。
以下是代码:
public class Main extends AppCompatActivity {
EditText convFromET, convToET, commaNumbET;
Spinner selectCatS, convFromS, convToS;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
convFromET = (EditText)findViewById(R.id.input_convert_from);
convToET = (EditText)findViewById(R.id.input_convert_to);
commaNumbET = (EditText)findViewById(R.id.input_comma_numbers);
selectCatS = (Spinner)findViewById(R.id.spinner_select_category);
convFromS = (Spinner)findViewById(R.id.spinner_convert_from);
convToS = (Spinner)findViewById(R.id.spinner_convert_to);
SetSelectCatSOnClickListener( );
}
private void SetSelectCatSOnClickListener( ){
selectCatS.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
ArrayAdapter<CharSequence> adapter;
switch( position ) {
case 0: // length
adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.spinner_length, android.R.layout.simple_spinner_item);
break;
case 1: // speed
adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.spinner_speed, android.R.layout.simple_spinner_item );
break;
case 2: //tempreture
adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.spinner_tempreture, android.R.layout.simple_spinner_item );
break;
case 3: // volume
adapter = ArrayAdapter.createFromResource( getApplicationContext(), R.array.spinner_volume, android.R.layout.simple_spinner_item );
break;
default: // Weigth
adapter = ArrayAdapter.createFromResource( getApplicationContext(), R.array.spinner_weigth, android.R.layout.simple_spinner_item );
break;
}
adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
convFromS.setAdapter( adapter );
convToS.setAdapter( adapter );
}
});
}
}
这是XML文件(strings.xml):
<string name="select_category">Choose a category</string>
<string name="convert_from">Convert from</string>
<string name="convert_to">Convert to</string>
<string name="numbers_after_comma">Numbers after comma</string>
<string-array name="spinner_categories">
<item>Length</item>
<item>Weigth</item>
<item>Volume</item>
<item>Tempreture</item>
<item>Speed</item>
<item>Volume</item>
</string-array>
<string-array name="spinner_length">
<item>Inches</item>
<item>Feets</item>
<item>Centimeters</item>
<item>Meters</item>
<item>Kilometers</item>
</string-array>
<string-array name="spinner_speed">
<item>Meters per second</item>
<item>Kilometers per hour</item>
<item>Miles per hour</item>
</string-array>
<string-array name="spinner_tempreture">
<item>Celsius</item>
<item>Farenheith</item>
<item>Kelvin</item>
</string-array>
<string-array name="spinner_volume">
<item>Cubic milimeters</item>
<item>Cubic centimeters</item>
<item>Cubic meters</item>
<item>Liters</item>
</string-array>
<string-array name="spinner_weigth">
<item>Miligrams</item>
<item>Grams</item>
<item>Kilograms</item>
<item>Pounds</item>
<item>Tons</item>
</string-array>
顺便说一下,当我在SetSelectCatSOnClickListener()
中评论onCreate
时,应用程序不会崩溃,因此我认为问题必定存在。
提前致谢!
答案 0 :(得分:0)
它不是OnItemClick它应该是微调器的onItemSelected
答案 1 :(得分:-1)
使用logcat 查找错误的具体信息。
关于日食:
转到窗口 - &gt;显示视图 - &gt; Android - &gt; logcat中。
然后将Logcat固定在屏幕的底部。
您会看到一个下拉列表默认为详细。将其更改为错误。现在,您只会在Logcat中看到错误。
如果您在此处发布错误,我相信我们可以为您提供帮助。