Android弹出窗口崩溃

时间:2017-12-13 03:08:30

标签: android

我想从我的UnosCertifikata类中显示一个弹出窗口。对于测试,我复制了这个代码,当我将它创建为一个独立的项目时,它真的有效,但我不能让它在我的应用程序上工作,它实际上包含片段。请查看此代码并建议我如何改进它。请不要评判我,因为我是编程初学者。

public class UnosCertifikata extends Activity
    implements OnItemClickListener {
EditText stcwCertifikat;
ListPopupWindow listPopupWindow;

// input desired list
String[] certifikat ={"Gmdss", "Mechanic", "Eletr.","Master",
        "Officer"};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_unos_certifikata);

    stcwCertifikat = findViewById(
            R.id.et_odabirSTCWCertifikata);
    listPopupWindow = new ListPopupWindow(
            UnosCertifikata.this);
    listPopupWindow.setAdapter(new ArrayAdapter<>(
            UnosCertifikata.this,
            R.layout.list_item, certifikat));
    listPopupWindow.setAnchorView(stcwCertifikat);


    listPopupWindow.setWidth(10000);
    listPopupWindow.setHeight(400);

    listPopupWindow.setModal(true);
    listPopupWindow.setOnItemClickListener(
            UnosCertifikata.this);
    stcwCertifikat.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            listPopupWindow.show();
        }
    });
}

@Override
public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
    stcwCertifikat.setText(certifikat[position]);
    listPopupWindow.dismiss();
}

}

log-cat在线给我错误

listPopupWindow.show();

这是我的日志...

12-13 07:00:18.786 2642-2642/com.example.robert.seamansbook E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.example.robert.seamansbook, PID: 2642
  java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
      at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:437)
      at android.widget.ArrayAdapter.getView(ArrayAdapter.java:407)
      at android.widget.AbsListView.obtainView(AbsListView.java:2372)
      at android.widget.DropDownListView.obtainView(DropDownListView.java:305)
      at android.widget.ListView.measureHeightOfChildren(ListView.java:1408)
      at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1257)
      at android.widget.ListPopupWindow.show(ListPopupWindow.java:613)
      at com.example.robert.seamansbook.UnosCertifikata$1.onClick(UnosCertifikata.java:46)
      at android.view.View.performClick(View.java:6256)
      at android.view.View$PerformClick.run(View.java:24697)
      at android.os.Handler.handleCallback(Handler.java:789)
      at android.os.Handler.dispatchMessage(Handler.java:98)
      at android.os.Looper.loop(Looper.java:164)
      at android.app.ActivityThread.main(ActivityThread.java:6541)
      at java.lang.reflect.Method.invoke(Native Method)
      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
   Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
      at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:424)
      at android.widget.ArrayAdapter.getView(ArrayAdapter.java:407) 
      at android.widget.AbsListView.obtainView(AbsListView.java:2372) 
      at android.widget.DropDownListView.obtainView(DropDownListView.java:305) 
      at android.widget.ListView.measureHeightOfChildren(ListView.java:1408) 
      at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1257) 
      at android.widget.ListPopupWindow.show(ListPopupWindow.java:613) 
      at com.example.robert.seamansbook.UnosCertifikata$1.onClick(UnosCertifikata.java:46) 
      at android.view.View.performClick(View.java:6256) 
      at android.view.View$PerformClick.run(View.java:24697) 
      at android.os.Handler.handleCallback(Handler.java:789) 
      at android.os.Handler.dispatchMessage(Handler.java:98) 
      at android.os.Looper.loop(Looper.java:164) 
      at android.app.ActivityThread.main(ActivityThread.java:6541) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

1 个答案:

答案 0 :(得分:0)

在&#34; list_item.xml上发现我的错误&#34; ... /必须格式化TextView以便ArrayAdapter可以工作...

...谢谢你们......

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:textStyle="bold" />