致命致命:主要错误

时间:2014-09-28 14:48:12

标签: android exception

以下是我的代码。当我尝试运行应用程序时,它给了我一个错误。它说主要有一个致命的例外。有人可以帮帮我吗?

这是我的main.java

    package com.go.nextgendevelopment.uefaeuro2016;

    import android.app.Activity;
    import android.app.ListActivity;
    import android.content.Context;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.ImageView;
    import android.widget.ListView;
    import android.widget.TextView;

    import java.util.List;


    public class main extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         //Adapter
        setListAdapter(new Myadapter(this,android.R.layout.simple_list_item_1, R.id.textView, getResources().getStringArray(R.array.teams)));
    }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private class Myadapter extends ArrayAdapter<String> {

        public Myadapter(Context context, int resource, int textViewResourceID, String[] objects) {
            super(context,resource,textViewResourceID,objects);
        }
        @Override
        public View getView(int position, View convertView,ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.list_item, parent, false);
            String[] items = getResources().getStringArray(R.array.teams);

            ImageView iv = (ImageView) row.findViewById(R.id.imageView);
            TextView tv = (TextView) row.findViewById(R.id.textView);

            tv.setText(items[position]);

            if (items[position].equals("ajax")){
                iv.setImageResource(R.drawable.logo_ajax);
            }
            else if (items[position].equals("feyenoord")){
                iv.setImageResource(R.drawable.logo_feyenoord);
            }


            return super.getView(position,convertView, parent);
        }
    }
    }

activity_main.xml中

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/list"
            android:layout_gravity="center_horizontal" />
    </LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

提前致谢!

1 个答案:

答案 0 :(得分:0)

  1. 删除onCreate()
  2. 中的setContentview
  3. 设置列表适配器时会传递listitem布局