抱歉我的英语不好。 我现在的目的是在Snackbar中显示一个editText和gridView文本。当用户单击FAB时,此Snackbar会显示,而不是可以在editText中编写内容。之后,用户应该单击gridView中显示的GridViewAdapter生成的一些文本。我的代码成功显示了editView和一些静态按钮the code,但它无法查看gridView。这是我的最后一段代码,请帮我找到解决方案
//grid view properties
GridView gridView;
List<String> ItemsList;
String selectedItem;
TextView GridViewItems,BackSelectedItem;
int backposition = -1;
String[] itemsName = new String[]{
"Fisika",
"Kimia",
"Biologi",
"Matematika",
};
private void setupUI() {
btnFab = (FloatingActionButton) findViewById(R.id.btnFloatingAction);
btnFab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CoordinatorLayout linearLayout = (CoordinatorLayout)findViewById(R.id.coordinatorLayout);
final Snackbar snackbar = Snackbar.make(linearLayout, "", Snackbar.LENGTH_INDEFINITE);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) snackbar.getView();
// Inflate your custom view with an Edit Text
LayoutInflater objLayoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View snackView = objLayoutInflater.inflate(R.layout.custom_snac_layout, null);
// custom_snac_layout is your custom xml
final EditText txt_event = (EditText) snackView.findViewById(R.id.event_text);
gridView = (GridView)findViewById(R.id.grid_view);
ItemsList = new ArrayList<String>(Arrays.asList(itemsName));
gridView.setAdapter(new GridViewAdapter(ParentActivity.this));
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
selectedItem = parent.getItemAtPosition(position).toString();
GridViewItems = (TextView) view;
GridViewItems.setBackgroundColor(Color.parseColor("#814f00"));
GridViewItems.setTextColor(Color.parseColor("#fdfcfa"));
BackSelectedItem = (TextView) gridView.getChildAt(backposition);
if (backposition != -1)
{
BackSelectedItem.setSelected(false);
BackSelectedItem.setBackgroundColor(Color.parseColor("#fbdcbb"));
BackSelectedItem.setTextColor(Color.parseColor("#040404"));
}
backposition = position;
String event = txt_event.getText().toString();
Toast.makeText(ParentActivity.this, "Item selected "+ selectedItem +"Text "+event, Toast.LENGTH_LONG).show();
}
});
layout.addView(snackView, 0);
snackbar.show();
}
});
}
public class GridViewAdapter extends BaseAdapter {
Context context;
public GridViewAdapter(Context context)
{
this.context = context;
}
@Override
public int getCount() {
return itemsName.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return itemsName[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
TextView text = new TextView(this.context);
text.setText(itemsName[position]);
text.setGravity(Gravity.CENTER);
text.setBackgroundColor(Color.parseColor("#fbdcbb"));
text.setTextColor(Color.parseColor("#040404"));
text.setLayoutParams(new GridView.LayoutParams(144, 144));
return text;
}
}
这里是我的custom_snac_layout.xml代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_alignParentTop="true"
android:id="@+id/event_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollHorizontally="false"
android:hint="@string/type_your_message"
android:inputType="textNoSuggestions"
android:maxLines="4"
android:singleLine="false"
android:textSize="18sp"
android:paddingLeft="4dp" />
<GridView
android:id="@+id/grid_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/event_text"
android:layout_centerHorizontal="true"
android:numColumns="3"/>
这些代码报告&#34; NullPointerException&#34;在logcat上
答案 0 :(得分:0)
试试这个,
gridView = (GridView)snackView.findViewById(R.id.grid_view);