选择微调器值时,android应用程序停止工作
Posted
技术标签:
【中文标题】选择微调器值时,android应用程序停止工作【英文标题】:android app stops working when spinner values are selected 【发布时间】:2015-10-02 21:53:24 【问题描述】:下面的代码有什么问题?选择“信息技术”分支和“第一”学期后单击“开始”,应用程序停止工作(因为我只在信息技术(第一学期)分支中提供了数据。
查看日志图像 - http://postimg.org/image/6h006a15b/
Spinner branch, semester;
Button go;
TextView branchText, semText;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
branch = (Spinner) findViewById(R.id.spinner1);
semester = (Spinner) findViewById(R.id.spinner2);
go = (Button) findViewById(R.id.button1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.Branch, android.R.layout.simple_spinner_item);
branch.setAdapter(adapter);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this,
R.array.Semester, android.R.layout.simple_spinner_item);
semester.setAdapter(adapter1);
branch.setOnItemSelectedListener(this);
semester.setOnItemSelectedListener(this);
go.setOnClickListener(this);
@Override
public void onClick(View arg0)
// TODO Auto-generated method stub
String check = branchText.getText().toString();
String check1 = semText.getText().toString();
if(check.equals("Information Technology")&&check1.equals("1st"))
Intent it=new Intent(MainActivity.this, IT1stSem.class);
startActivity(it);
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id)
// TODO Auto-generated method stub
switch (parent.getId())
case R.id.spinner1:
branchText = (TextView) view;
Toast.makeText(this, branchText.getText(), Toast.LENGTH_SHORT).show();
break;
case R.id.spinner2:
semText = (TextView) view;
Toast.makeText(this, semText.getText(), Toast.LENGTH_SHORT).show();
break;
@Override
public void onNothingSelected(AdapterView<?> arg0)
// TODO Auto-generated method stub
//IT1stSem.java
public class IT1stSem extends ListActivity
String[] data = "Mathematics-I", "Physics-I", "Chemistry", "Engg. Mechanics", "Electrical Science";
ListView l;
@Override
protected void onCreate(Bundle savedInstanceState)
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.it1stsem);
l= (ListView) findViewById(R.id.listView1);
//=getListView();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
l.setAdapter(adapter);
@Override
protected void onListItemClick(ListView l, View v, int position, long id)
// TODO Auto-generated method stub
TextView temp = (TextView) v;
Toast.makeText(this, ""+temp.getText()+ " "+position, Toast.LENGTH_SHORT).show();
//值-strings.xml
<?xml version="1.0" encoding="utf-8"?>
<string name="app_name">KiiT Syllabus</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string-array name="Branch" >
<item >Civil</item>
<item >Mechanical</item>
<item >Automobile</item>
<item >Electrical</item>
<item >Electronics & Tele-Communication</item>
<item >Computer Science</item>
<item >Information Technology</item>
<item >Electronics & Electrical</item>
<item >Electronics & Instrumentation</item>
</string-array>
<string-array name="Semester">
<item >1st</item>
<item >2nd</item>
<item >3rd</item>
<item >4th</item>
<item >5th</item>
<item >6th</item>
<item >7th</item>
<item >8th</item>
</string-array>
【问题讨论】:
你必须分享日志 @MohamedHamdaoui 在问题中添加了日志图像(链接) 我认为应该是view.getId())
而不是parent.getId())
@Rustam 不起作用! ://
在onItemSelected
中放一个断点并调试
【参考方案1】:
//setContentView(R.layout.it1stsem);
为什么要注释掉这一行?我认为这就是为什么它找不到 listView1 而你得到 NullPointerException。尝试取消注释此行。
【讨论】:
我什至没有评论就试过了。但问题还是一样 @AnkushKapoor 从您的图片中很难看出问题出在哪里。哪一行代码?以上是关于选择微调器值时,android应用程序停止工作的主要内容,如果未能解决你的问题,请参考以下文章