午夜学徒xpf 2016-10-02 10:17 采纳率: 87.5%
浏览 2270

新手,下拉菜单选择,运行时出现闪退.

--------------------MainActivity--------------------
package com.example.example3_2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
final static int WRAP_CONTENT=-2;
int []image={R.mipmap.basketball,R.mipmap.football,R.mipmap.volleyball};
int []msgid={R.string.zuqiu,R.string.lanqiu,R.string.pingpangqiu};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner=(Spinner)this.findViewById(R.id.spinner);
BaseAdapter baseAdapter=new BaseAdapter() { //设置适配器
@Override
public int getCount() {
return 3; //总共三项
}

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LinearLayout linearLayout=new LinearLayout(MainActivity.this);
            linearLayout.setOrientation(LinearLayout.HORIZONTAL);
            ImageView imageView=new ImageView(MainActivity.this);
            imageView.setImageDrawable(getResources().getDrawable(image[position]));
            linearLayout.addView(imageView);        //添加到LinearLayout
            TextView textView=new TextView(MainActivity.this);
            textView.setText(""+getResources().getText(msgid[position]));       //设置内容
            textView.setTextSize(24);
            textView.setTextColor(getResources().getColor(R.color.colorPrimary));   //设置字体颜色
            linearLayout.addView(textView);     //添加到LinearLayout
            return linearLayout;
        }
    };
    spinner.setAdapter(baseAdapter);
    spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            TextView textView=(TextView)findViewById(R.id.text_view);
            LinearLayout linearLayout=(LinearLayout)view;   //获取选中的Linearlayout
            TextView textView1=(TextView)linearLayout.getChildAt(1);    //获取其中的TextView
            StringBuilder stringBuilder=new StringBuilder();
            stringBuilder.append(getResources().getText(R.string.ys));
            stringBuilder.append(":");
            stringBuilder.append(textView1.getText());
            textView.setText(stringBuilder.toString());
        }
        public void onNothingSelected(AdapterView<?>parent){}   //重写的方法
    });
}

}
------------------------main.XML---------------------
<?xml version="1.0" encoding="utf-8"?>
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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.example.example3_2.MainActivity">

<TextView
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ys" />
<Spinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</Spinner>


------------------------string.XML-------------------
<?xml version="1.0" encoding="utf-8"?>
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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.example.example3_2.MainActivity">

<TextView
    android:id="@+id/text_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ys" />
<Spinner
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</Spinner>


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

#3F51B5
#303F9F
#FF4081

  • 写回答

1条回答 默认 最新

报告相同问题?

悬赏问题

  • ¥100 需要跳转番茄畅听app的adb命令
  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证