我尝试在我的活动中添加一个Spinner,到目前为止代码中没有错误,但每次我在Android模拟器上运行它时,它都会关闭。请帮助。
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) {
}
}
Spinner代码部分
@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) {
}
}
我试图摆脱所有处理Spinner的代码,并且Activity在.xml中使用Spinner工作正常,所以我很确定我的.xml不是问题,所以你可以专注于代码本身。
谢谢。
答案 0 :(得分:1)
您正在尝试在创建活动对象时找到Spinner
。在这种情况下,您将获得null
。调用onCreate
setContentView
方法
Spinner spinner = (Spinner) findViewById(R.id.spinner);
TextView txt = (TextView) findViewById(R.id.txt);