Android应用程序停止运行此代码。怎么解决?

时间:2015-07-19 08:38:39

标签: android android-listview android-spinner

此代码有问题吗? 应用停止工作。在这里查看日志图像 http://postimg.org/image/dk503e5rj/ 我猜onCreate方法有问题。请检查一下。

 package com.example.kiitsyllabus;
 import android.app.ListActivity;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.ArrayAdapter;
 import android.widget.ListView;
 import android.widget.TextView;
 import android.widget.Toast;

  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.List);
    //l=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();
}


}

MainActivity.java

package com.example.kiitsyllabus;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnItemSelectedListener,
    OnClickListener {

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

}

}

1 个答案:

答案 0 :(得分:0)

您在Log cat中有原因

Your content must have a ListView whose id attribute is 'android.R.id.list'