iT邦幫忙

0

昨天有詢問過的,android 點擊進入下一個頁面變空白,莫名停止運行

這次有用logcat擷取錯誤(最下方)
用的是android studio,產生apk時沒有出現錯誤,但運行時錯誤就開始了
畫面A有三個按鈕分別點擊Intent到另一個activity。
但第三個(btn5)點擊進入後畫面變成空白,程式也會停止運行

btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_rd = new Intent();
                intent_rd.setClass(Studenthome.this, record.class);
                startActivity(intent_rd);
                    }
                });
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_rr = new Intent(); 
                intent_rr.setClass(Studenthome.this,repair.class);
                startActivity(intent_rr);
            }
        });
        btn5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent intent_lv =new Intent();
               intent_lv.setClass(Studenthome.this,leave.class);
               startActivity(intent_lv);
            }
        });

google問題是說可能Androidmanifest.xml沒有加入語句android:theme
(查看後都有加入)
或是public void onCreate(Bundle savedInstanceState, PersistableBundleState) 多了PersistableBundle的參數(沒有多出這串參數)

畫面空白的activity程式

public class leave extends AppCompatActivity {
    public static TextView leave;
    String url_leave="";//網址因為不方便列出先刪除
    ListView LV_leave;
    String cookieStr,id_text;
    @Override

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_leave);
        

   Handler myHandler_leave = new Handler();//Handler為時間處理器
   myHandler_leave.postDelayed(runTimerStop_leave, 5000); 
        if (cookieStr != null) {
            myHandler_leave.removeCallbacks(runTimerStop_leave); 
        }
        LV_leave = (ListView) findViewById(R.id.leave_list);
    }


    private Runnable runTimerStop_leave = new Runnable() {
        @Override
        public void run() {
            select(null);
        }
    };

    //顯示資料
    

public void select(String id_leave) {
   try {
      String r_leave = DBphp_leave.DBstring_leave(id_leave, url_leave);
      JSONArray jsonArray_leave = new JSONArray(r_leave);
      List<Map<String, Object>> items = new ArrayList<Map<String, Object>>();

      for (int i_leave = 0; i_leave < jsonArray_leave.length(); i_leave++) {
         JSONObject jsonData_leave = jsonArray_leave.getJSONObject(i_leave);
         Map<String, Object> item = new HashMap<String, Object>();
         
        item.put("lv_which_day ", jsonData_leave.getString("lv_which_day "));
        item.put("lv_date",jsonData_leave.getString("lv_date"));
        item.put("lv_reason",jsonData_leave.getString("lv_reason"));
        item.put("lv_state",jsonData_leave.getString("lv_state"));
        items.add(item);
            }

SimpleAdapter SA_leave = new SimpleAdapter(this, items, R.layout.list_text, new String[]{"lv_which_day ","lv_date","lv_reason","lv_state"}, new int[]{R.id.text0, R.id.text1, R.id.text2,R.id.text3});

            LV_leave.setAdapter(SA_leave);


        } catch (Exception e) {
            Log.e("log_tag=", e.toString());//log.e為發送錯誤訊息
        }
    }

}

連上手機模擬器執行出現畫面空白的頁面後......logcat的顯示
https://upload.cc/i1/2020/02/21/LAqbwn.jpg
訊息不斷冒出,貌似還是重複的,只到我關閉apk訊息才停止輸出
擷取其中一段
基本就是這段訊息不斷重複
https://upload.cc/i1/2020/02/21/U8shHQ.jpg

Caused by: java.lang.ClassCastException: androidx.recyclerview.widget.RecyclerView cannot be cast to android.widget.ListView
        at com.skypan.school.leave.onCreate(leave.java:59)
        at android.app.Activity.performCreate(Activity.java:6787)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2619)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6125) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783) 
2020-02-21 17:12:14.235 1805-1805/? E/AndroidRuntime: in writeCrashedAppName, pkgName :com.skypan.school
2020-02-21 17:12:14.237 1805-1805/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.skypan.school, PID: 1805
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skypan.school/com.skypan.school.leave}: java.lang.ClassCastException: androidx.recyclerview.widget.RecyclerView cannot be cast to android.widget.ListView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2666)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2727)
        at android.app.ActivityThread.-wrap12(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1478)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6125)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:893)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:783)

(leave.java:59),leave.java的59行
https://upload.cc/i1/2020/02/21/VZGt6c.jpg
https://upload.cc/i1/2020/02/21/aC1dAM.jpg
LV_leave = (ListView) findViewById(R.id.leave_list);
不太明白這裡會錯誤的原因,在想是不是xml有不行的地方.....
androidx.recyclerview.widget.RecyclerView cannot be cast to android.widget.ListView
https://upload.cc/i1/2020/02/21/vNezjB.jpg
直接從palette拉出來的listview

剩下的
(Activity.java:6787)
(Handler.java:102)
(Looper.java:154)
(Instrumentation.java:1146)都沒碰過的東西,好像都是一開始內建的???

1 個回答

2
海綿寶寶
iT邦大神 1 級 ‧ 2020-02-21 22:59:47
最佳解答

Log 說得很清楚:androidx.recyclerview.widget.RecyclerView cannot be cast to android.widget.ListView

那你就把目前使用的 RecyclerView

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/leave_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

改成

<ListView
    android:id="@+id/leave_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

試看看

成功了,之前都是直接拉出來,沒有自己打上去試試,原來直接改listview就行........
謝謝

問題解決就好
/images/emoticon/emoticon12.gif

我要發表回答

立即登入回答