私有 ArrayAdapter 在 onLoaderFinished 方法中不可见

private ArrayAdapter not visible in onLoaderFinished method

我正在开发 android,但在简单概念上遇到了问题。我不明白为什么 activity class 的私有 ArrayAdapter 在 onLoadFinished 方法中不是 "in scope"。

我有;

public class DestinationListActivity extends ActionBarActivity
    implements LoaderManager.LoaderCallbacks<Cursor>
{
    // Sub-Activity static return code identifiers
    private static final int SUBACTIVITY_SINGLEDESTINATION = 1;

    // Create the array list of destinations
    private ArrayList<String> vArrayList_Destinations = new ArrayList<String>();

    // Create the Array Adapter to bind the array to the List View
    private ArrayAdapter<String> vArrayAdapter_Destinations;

随着onCreate;

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

// Inflate the view
setContentView(R.layout.destinationlistactivity);

// Set references to the widgets
Button vButton_AddNew=(Button)findViewById(R.id.destinationlistactivity_Button_AddNew);
ListView vListView_DestinationList=(ListView)findViewById(R.id.destinationlistactivity_ListView_DestinationList);

// Create the array list of destinations
//ArrayList<String> vArrayList_Destinations = new ArrayList<String>();

// Create the Array Adapter to bind the array to the List View
ArrayAdapter<String> vArrayAdapter_Destinations = new ArrayAdapter<String>
        (this,
         android.R.layout.simple_list_item_1,
         vArrayList_Destinations);

// Bind the array adapter to the list view
vListView_DestinationList.setAdapter(vArrayAdapter_Destinations);

getLoaderManager().initLoader(0, null, this);
}

和onLoadFinished;

public void onLoadFinished(Loader<Cursor> pLoader, Cursor pCursor)
{
int keyDestinationIndex = pCursor.getColumnIndexOrThrow(DestinationContentProvider.DESTINATION_NAME);

vArrayList_Destinations.clear();
while (pCursor.moveToNext())
{
    vArrayList_Destinations.add(pCursor.getString(keyDestinationIndex));
}
vArrayAdapter_Destinations.notifyDataSetChanged();
}

我调试的时候,来到vArrayAdapter_Destinations.notifyDataSetChanged();行,vArrayAdapter_Destinations 为空,我得到一个 NULL 指针异常。

如何超出范围?

我不太了解 Loader,但我可以看出你没有超出范围,如果 vArrayAdapter_Destinations 为 null,那么你需要在调用 [=25= 时获取 NullPointerException ]() 正确的?我会先深入研究一下。另外,当你实现 LoaderCallback 时,你还需要实现其他方法,对吧? onCreateLoader 和 onLoaderReset,确保它们有正确的实现。

编辑

这里有这段代码

// Create the Array Adapter to bind the array to the List View
ArrayAdapter<String> vArrayAdapter_Destinations = new ArrayAdapter<String>
    (this,
     android.R.layout.simple_list_item_1,
     vArrayList_Destinations);

必须是这个

// Create the Array Adapter to bind the array to the List View
vArrayAdapter_Destinations = new ArrayAdapter<String>
    (this,
     android.R.layout.simple_list_item_1,
     vArrayList_Destinations);

您已经有了变量,您只需要使用它。如果您将 class 名称放在开头,它将导致创建一个新变量。这叫做variable scope

您实际上有两个同名变量,一个在 class 范围内,另一个在您的 onCreate 方法的方法范围内。 您正在创建的 ArrayAdapter 在您的 onCreate 方法完成后消失了。所以 onLoadFinished 方法试图访问仍然为 null 的全局变量并且异常是正确的。

new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, vArrayList_Destinations); 分配给您的全局变量,您应该没问题。

编辑: 为了让您更清楚地了解变量作用域,我们只看作用域:

-DestinationListActivity
|
|-- (1) vArrayAdapter_Destinations (初始化为null)
|
|---- onCreate
| |
| | ---- (2)vArrayAdapter_Destinations(初始化为new ArrayAdapter<String>(...);
|
|---- onLoadFinished
| |
| | ---- 这里是 (1) 对 vArrayAdapter_Destinations 的调用,因为 (2) 超出范围

希望这可以帮助您更好地理解您的问题。