第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

空對(duì)象引用上的 android.widget.ListView.setAdapter

空對(duì)象引用上的 android.widget.ListView.setAdapter

慕尼黑8549860 2023-10-13 10:19:16
錯(cuò)誤發(fā)生在 setAdapter 行上。我找不到錯(cuò)誤在哪里。它與空異常無(wú)關(guān)。有些事情不正常java.lang.RuntimeException:無(wú)法啟動(dòng)活動(dòng) ComponentInfo{com.example.myapplication/com.example.myapplication.Namelist}:java.lang.NullPointerException:嘗試調(diào)用虛擬方法 'void android.widget.ListView.setAdapter(android. widget.ListAdapter)' 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913) 處的 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 處的空對(duì)象引用上 android.app.servertransaction.LaunchActivityItem.execute (LaunchActivityItem.java:78)位于主要類(lèi)別:public class Namelist extends AppCompatActivity {String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry",        "WebOS","Ubuntu","Windows7","Max OS X"};ListView l1;ArrayAdapter<String> arrayAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {    l1 = (ListView) findViewById(R.id.list);    arrayAdapter = new ArrayAdapter<String>(Namelist.this,R.layout.activity_namelist,mobileArray);   l1.setAdapter(arrayAdapter);    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_namelist);}}活動(dòng)XML:<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".Namelist"><ListView    android:id="@+id/list"    android:layout_width="409dp"    android:layout_height="729dp"    app:layout_constraintBottom_toBottomOf="parent"    app:layout_constraintEnd_toEndOf="parent"    app:layout_constraintStart_toStartOf="parent"    app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>提前致謝
查看完整描述

1 回答

?
qq_遁去的一_1

TA貢獻(xiàn)1725條經(jīng)驗(yàn) 獲得超8個(gè)贊

l1之前不能引用setContentView(R.layout.activity_namelist)


正確的代碼在這里:


public class Namelist extends AppCompatActivity {


    String[] mobileArray = {"Android","IPhone","WindowsMobile","Blackberry","WebOS","Ubuntu","Windows7","Max OS X"};

    ListView l1;

    ArrayAdapter<String> arrayAdapter;


    @Override

    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_namelist);


        l1 = findViewById(R.id.list);


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

       l1.setAdapter(arrayAdapter);

    }

}

并且您還將錯(cuò)誤傳遞layout R.layout.activity_namelist給了 的第二個(gè)參數(shù)ArrayAdapter。您沒(méi)有聲明布局名稱(chēng)activity_namelist。因此,您可以使用默認(rèn)的內(nèi)置布局android.R.layout.simple_list_item_1詢問(wèn)上面的代碼。


查看完整回答
反對(duì) 回復(fù) 2023-10-13
  • 1 回答
  • 0 關(guān)注
  • 112 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)