选择微调器值时,android app停止工作

时间:2015-07-14 10:06:40

标签: android android-intent android-listview

以下代码中的问题是什么?选择“信息技术”分支和“第一”学期后点击Go后,应用程序停止工作(因为我只在信息技术(第一个Sem)分支中提供数据。

签出日志图片 - 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();
}

}

// Values - 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 &amp; Tele-Communication</item>
    <item >Computer Science</item>
    <item >Information Technology</item>
    <item >Electronics &amp; Electrical</item>
    <item >Electronics &amp; 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>

1 个答案:

答案 0 :(得分:0)

//的setContentView(R.layout.it1stsem);

你为什么评论这一行?我认为这就是为什么它找不到listView1而你得到NullPointerException。尝试取消注释这一行。