我将列表视图附加到FAB,但Android Studio显示以下错误:
无法解析setAdapter和AttachToListView方法中的符号
我尝试重建和关闭并重新开放项目。
代码:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
String[] values = new String[]{"Change message", "Change picture"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.AttachToListView(listView);
}
XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/androidparty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
<EditText
android:id="@+id/birthday_message"
android:text="Many many happy returns of the day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="36sp"
android:fontFamily="sans-serif-light"
android:layout_margin="20dp"
android:focusable="true"
android:focusableInTouchMode="true"/>
<EditText
android:id="@+id/from_text"
android:text="From, "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:textSize="36sp"
android:textColor="@android:color/white"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="20dp"/>
<com.melnykov.fab.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/from_text"
android:layout_alignParentRight="true"
android:layout_marginRight="16dp"
android:src="@drawable/ic_action_content_new"
app:fab_colorNormal="#424242"
android:textAllCaps="true"/>
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/list" />
</RelativeLayout>
答案 0 :(得分:2)
您已在onCreate()
之外编写了所有代码。正如codeMagic所说,移动所有代码如下所示
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] values = new String[]{"Change message", "Change picture"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, values);
ListView listView = (ListView) findViewById(android.R.id.list);
listView.setAdapter(adapter);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.AttachToListView(listView);
}
}