YXTS122
YXTS122
2016-12-26 22:19
采纳率: 100%
浏览 1.4k

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条回答 默认 最新

  • stone_you
    stone_you 2016-12-27 02:31
    已采纳

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

    点赞 评论
  • YXTS122
    YXTS122 2016-12-26 22:19

    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>
    
    
    点赞 评论
  • baidu_31405631
    rongyili88 2016-12-27 00:50

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

    点赞 评论
  • lxk_1993
    103style 2016-12-27 01:14

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

    点赞 评论
  • stone_you
    stone_you 2016-12-27 02:23

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

    点赞 评论
  • kong_gu_you_lan
    容华谢后 2016-12-27 02:27

    把下面这行代码的实例化下载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);
        ...
    }
    
    点赞 评论

相关推荐