android,刚点debug就崩溃了

图片说明
MainActivity.java

 package com.emple.divide;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;

public class MainActivity extends Activity {
    private String[] str=new String[]{"one","two","three","four"};
    private GridView gv;
    private TextView tv;
    private Button bt;
    ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,str);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gv=(GridView)findViewById(R.id.gv);
        tv=(TextView)findViewById(R.id.tv);
        bt=(Button)findViewById(R.id.bt);
        bt.setOnClickListener(new bt_click());
        gv.setOnItemClickListener(new gv_click());
    }

    class bt_click implements OnClickListener
    {
        public void onClick(View v)
        {
            gv.setNumColumns(2);
            gv.setAdapter(arrayAdapter);
        }
    }

    class gv_click implements OnItemClickListener
    {
        public void onItemClick(AdapterView<?> parent,View v,int position,long arg3)
        {
            switch (arrayAdapter.getCount())
            {
            case 4:
                tv.setText(str[position]);
                break;
            default:break;
            }
        }
    }

    @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);
    }
}

6个回答

arrayadapter初始化过早,arrayadapter在activity类实例化的话的时候就初始化了,arrayadapter初始化的时候传入的this是一个context,这个时候context还是无效的,需要在oncreate调用之后才对arrayadapter进行初始化,放在supo.oncreate之后,或者放在onresume里面都可以

activity_main.xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.emple.divide.MainActivity" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <GridView 
        android:id="@+id/gv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

    </GridView>

    <Button 
        android:id="@+id/bt"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/num" />

</RelativeLayout>

R.layout._simple_list_item_1的代码能贴一下吗?看样是AMS状态不对导致的

YXTS122
YXTS122 R.layout._simple_list_item_1的代码不是我写的,是系统的,我不知道在哪里
3 年多之前 回复

试试 把 arrayadapter 放到 gv_click 里面去试试, 不要把他设置为全局变量,或者 在 点击事件的时候在初始化

arrayadapter初始化过早,arrayadapter初始化的时候传入的this是一个context,这个时候context还是无效的,需要在oncreate调用之后才对arrayadapter进行初始化

把下面这行代码的实例化下载onCreate方法中

 ArrayAdapter<String> arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,str);

改成

private ArrayAdapter<String> arrayAdapter;

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

    arrayAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,str);
    ...
}
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐