0

我在安卓中使用 AIDE。当我使用正确的代码时,它也会多次给我消息。其中有什么问题?

package com.mycompany.listview;

import android.app.*;
import android.os.*;
import android.widget.*;
import java.util.*;
import android.text.*;

public class MainActivity extends Activity 
{

ListView myListView=(ListView)findViewById(R.id.myListView);

ArrayList<String> myList=new ArrayList<String>();

myList.add("khan");
myList.add("Gul");

ArrayAdapter<String> arrays=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,myList);

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}
}
4

1 回答 1

1

将您的代码放入onCreate()回调中:-

ListView myListView=(ListView)findViewById(R.id.myListView);

ArrayList<String> myList=new ArrayList<String>();

myList.add("khan");
myList.add("Gul");

ArrayAdapter<String> arrays=new ArrayAdapter<String>(this, 
android.R.layout.simple_list_item_1,myList);

像这样说:-

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ListView myListView=(ListView)findViewById(R.id.myListView);

    ArrayList<String> myList=new ArrayList<String>();

    myList.add("khan");
    myList.add("Gul");

    ArrayAdapter<String> arrays=new ArrayAdapter<String>(this, 
    android.R.layout.simple_list_item_1,myList);
}
于 2018-07-04T09:21:59.437 回答