“令牌上的语法错误”;“,,期望”在私有...适配器之后

时间:2021-02-14 22:32:48

It writes syntax error on token ";" ,, expected after the line private ArrayAdapter adapter ;

它在令牌“;”上写入语法错误,,预期后行私有ArrayAdapter适配器;

package ru.for_listactivity;

import android.os.Bundle;
import android.app.ListActivity;
import android.view.Menu;
import android.widget.ArrayAdapter;

public class MainActivity extends ListActivity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

    final String[] catnames = new String[] { "Рыжик", "Барсик", "Мурзик",
    "Мурка", "Васьк", "Томасина", "Бобик", "Кристина", "Пушок",
    "Дымка", "Кузя", "Китти", "Барбос", "Масяня", "Симба" } ;

private ArrayAdapter<String> adapter ; // here is a problem

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

} 

2 个解决方案

#1


3  

I think the problem is that you have code outside of the methods, and not inside a static initializer block.

我认为问题是你有方法之外的代码,而不是静态初始化程序块。

Try this:

private ArrayAdapter<String> adapter = 
    new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, catnames);

#2


1  

This is fine:

这可以:

private ArrayAdapter<String> adapter ; 

It declares adapter so it can be used by any method contained in the class.

它声明了适配器,因此它可以被类中包含的任何方法使用。

This however, is not:

但是,这不是:

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

It needs to be inside a method (perhaps you want it in onCreate?), not just in the class:

它需要在一个方法内(也许你想在onCreate中?),而不仅仅是在类中:

#1


3  

I think the problem is that you have code outside of the methods, and not inside a static initializer block.

我认为问题是你有方法之外的代码,而不是静态初始化程序块。

Try this:

private ArrayAdapter<String> adapter = 
    new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, catnames);

#2


1  

This is fine:

这可以:

private ArrayAdapter<String> adapter ; 

It declares adapter so it can be used by any method contained in the class.

它声明了适配器,因此它可以被类中包含的任何方法使用。

This however, is not:

但是,这不是:

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

It needs to be inside a method (perhaps you want it in onCreate?), not just in the class:

它需要在一个方法内(也许你想在onCreate中?),而不仅仅是在类中: