我正在尝试在单击列表中的项目时加载片段。项目文本是“营养素/目标计算器”。单击MainActivity.java文件中的列表项时,程序崩溃。
MainActivity.java
package com.example.crims.fooddiarytools;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends ListActivity {
private ListViewCustomAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter = new ListViewCustomAdapter(this);
mAdapter.addSectionHeaderItem("Food");
mAdapter.addItem("Nutrient/Target calculator");
ListView lv = (ListView) findViewById(android.R.id.list);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView parent, View v,
int position, long id) {
String itemInList = (String)parent.getItemAtPosition(position);
if(itemInList.equals("Nutrient/Target calculator")) {
// this won't load it crashes //////////////////////////////
Fragment fragment = new NutrientTargetCalculator();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.list, fragment);
ft.commit();
}
Toast.makeText(getBaseContext(), itemInList, Toast.LENGTH_SHORT).show();
mAdapter.myCliquedPosition = position;
mAdapter.notifyDataSetInvalidated();
}
});
}
}
NutrientTargetCalculator.java
package com.example.crims.fooddiarytools;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class NutrientTargetCalculator extends Fragment{
private static Button buttonNutrient;
private static Button buttonNutrientTarget;
private static Spinner foodUnitSpinner;
private static Spinner foodNutrientsSpinner;
private static TextView editTextNutrients;
private static TextView editTextServingSize;
private static TextView editTextScaleWeight;
private static TextView editTextNutrientOutput;
private static TextView editTextNutrientTarget;
private static TextView textViewServingOutPut1;
View myView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.nutrient_target_calculator, container, false);
return myView;
}
}
ListViewCustomAdapter.java
package com.example.crims.fooddiarytools;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.TreeSet;
/**
* Created by crims on 12/22/2016.
*/
public class ListViewCustomAdapter extends BaseAdapter{
private static final int TYPE_ITEM = 0;
private static final int TYPE_SEPARATOR = 1;
private ArrayList<String> mData = new ArrayList<String>();
private TreeSet<Integer> sectionHeader = new TreeSet<Integer>();
private LayoutInflater mInflater;
public int myCliquedPosition;
public ListViewCustomAdapter(Context context) {
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(final String item) {
mData.add(item);
notifyDataSetChanged();
}
public void addSectionHeaderItem(final String item) {
mData.add(item);
sectionHeader.add(mData.size() - 1);
notifyDataSetChanged();
}
@Override
public int getItemViewType(int position) {
return sectionHeader.contains(position) ? TYPE_SEPARATOR : TYPE_ITEM;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public String getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView textView;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
int rowType = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (rowType) {
case TYPE_ITEM:
convertView = mInflater.inflate(R.layout.list_view_text, null);
holder.textView = (TextView) convertView.findViewById(R.id.text);
break;
case TYPE_SEPARATOR:
convertView = mInflater.inflate(R.layout.list_view_box, null);
holder.textView = (TextView) convertView.findViewById(R.id.textSeparator);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.textView.setText(mData.get(position));
return convertView;
}
}
这是输出
12-22 23:36:43.716 4518-4518/? I/art: Not late-enabling -Xcheck:jni (already on)
12-22 23:36:43.922 4518-4518/com.example.crims.testing W/System: ClassLoader referenced unknown path: /data/app/com.example.crims.testing-1/lib/x86
12-22 23:36:43.926 4518-4518/com.example.crims.testing I/InstantRun: Instant Run Runtime started. Android package is com.example.crims.testing, real application class is null.
12-22 23:36:45.027 4518-4518/com.example.crims.testing W/System: ClassLoader referenced unknown path: /data/app/com.example.crims.testing-1/lib/x86
12-22 23:36:45.257 4518-4518/com.example.crims.testing W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
12-22 23:36:45.556 4518-4542/com.example.crims.testing D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
12-22 23:36:45.612 4518-4542/com.example.crims.testing I/OpenGLRenderer: Initialized EGL, version 1.4
12-22 23:36:45.673 4518-4542/com.example.crims.testing E/EGL_emulation: tid 4542: eglSurfaceAttrib(1165): error 0x3009 (EGL_BAD_MATCH)
12-22 23:36:45.673 4518-4542/com.example.crims.testing W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaa413620, error=EGL_BAD_MATCH
12-22 23:37:06.989 4518-4542/com.example.crims.testing E/Surface: getSlotFromBufferLocked: unknown buffer: 0xae4710e0
12-22 23:37:10.167 4518-4542/com.example.crims.testing E/EGL_emulation: tid 4542: eglSurfaceAttrib(1165): error 0x3009 (EGL_BAD_MATCH)
12-22 23:37:10.167 4518-4542/com.example.crims.testing W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaa413620, error=EGL_BAD_MATCH
12-22 23:38:40.868 4518-4542/com.example.crims.testing E/Surface: getSlotFromBufferLocked: unknown buffer: 0xaa1d5170
12-22 23:38:45.673 4518-4542/com.example.crims.testing E/EGL_emulation: tid 4542: eglSurfaceAttrib(1165): error 0x3009 (EGL_BAD_MATCH)
12-22 23:38:45.673 4518-4542/com.example.crims.testing W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaa413620, error=EGL_BAD_MATCH
12-22 23:40:15.359 4518-4542/com.example.crims.testing E/Surface: getSlotFromBufferLocked: unknown buffer: 0xae4710e0
12-22 23:40:44.665 4518-4542/com.example.crims.testing E/EGL_emulation: tid 4542: eglSurfaceAttrib(1165): error 0x3009 (EGL_BAD_MATCH)
12-22 23:40:44.665 4518-4542/com.example.crims.testing W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaa413620, error=EGL_BAD_MATCH
12-22 23:43:30.414 4518-4542/com.example.crims.testing E/Surface: getSlotFromBufferLocked: unknown buffer: 0xae4710e0
12-22 23:43:34.790 4518-4542/com.example.crims.testing E/EGL_emulation: tid 4542: eglSurfaceAttrib(1165): error 0x3009 (EGL_BAD_MATCH)
12-22 23:43:34.790 4518-4542/com.example.crims.testing W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaa413620, error=EGL_BAD_MATCH
答案 0 :(得分:3)
您无法替换 android.R.id.list 。使用以下步骤:
ft.replace(android.R.id.list,fragment);
带
ft.replace(R.id.list_wrapper,fragment);