1
我嘗試將Spinner添加到我的活動中,到目前爲止代碼中沒有錯誤,但每次我在Android模擬器上運行它時,都會強行關閉。請幫忙。Android Spinner Force關閉
package com.nhan.quach.random_number_creator;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class giac_mo extends ActionBarActivity implements AdapterView.OnItemSelectedListener {
private Toolbar toolbar;
private TextView text;
int backButtonCount = 0;
String[] s = {"1","2","3","4"};
Spinner spinner = (Spinner) findViewById(R.id.spinner);
TextView txt = (TextView) findViewById(R.id.txt);
public void onBackPressed() {
if (backButtonCount >= 1) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else {
Toast.makeText(this, "Tap Back again to exit.", Toast.LENGTH_SHORT).show();
backButtonCount++;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_giac_mo);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
text = (TextView) findViewById(R.id.textView);
Button button = (Button) findViewById(R.id.button);
NavigatetionDrawerFragment drawerFragment = (NavigatetionDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setup((DrawerLayout) findViewById(R.id.drawerlayout), toolbar);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, s);
adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(), ""+s[i], Toast.LENGTH_SHORT);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
微調代碼部分
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_giac_mo);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, s);
adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(), ""+s[i], Toast.LENGTH_SHORT);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
我試圖擺脫所有這些處理微調代碼和活動中的.xml工作正常,事件與微調等等我很確定我的.xml在這裏不是問題,所以你可以只關注代碼本身。
謝謝。
你可以展示時,應用程序崩潰輸出logcat的,所以我們可以看到是什麼原因造成 – o0rebelious0o