Java 在导航抽屉布局中为片段使用布局文件
我有一个android应用程序代码,它使用导航抽屉布局,以片段为主要内容。 到目前为止,我已经为抽屉中的所有片段项目创建了一个特定的片段类。现在我有一个不同的布局,我想得到作为一个项目在我的抽屉以及。布局是一个LiniarLayout,在xml characteristic.xml中定义Java 在导航抽屉布局中为片段使用布局文件,java,android,Java,Android,我有一个android应用程序代码,它使用导航抽屉布局,以片段为主要内容。 到目前为止,我已经为抽屉中的所有片段项目创建了一个特定的片段类。现在我有一个不同的布局,我想得到作为一个项目在我的抽屉以及。布局是一个LiniarLayout,在xml characteristic.xml中定义 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vert
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_device_address"
android:textSize="18sp"/>
<Space android:layout_width="5dp"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/device_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_state"
android:textSize="18sp"/>
<Space android:layout_width="5dp"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/connection_state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/disconnected"
android:textSize="18sp"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/label_data"
android:textSize="18sp"/>
<Space android:layout_width="5dp"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/data_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/no_data"
android:textSize="18sp"/>
</LinearLayout>
<ExpandableListView android:id="@+id/gatt_services_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
我希望能够使用它在片段之间切换(除了新的片段3外,它工作得很好):
但是,它似乎不起作用:
mGattServicesList.setOnChildClickListener(ServicesListClickListener);
失败于
E/AndroidRuntime(30644):由以下原因引起:java.lang.NullPointerException:尝试调用空对象引用上的虚拟方法“void android.widget.ExpandableListView.setOnChildClickListener(android.widget.ExpandableListView$OnChildClickListener)”
有人能告诉我我做错了什么吗
mGattServicesList
是否在activity\u main
layout中定义?mGattServicesList是我的主活动类中的一个变量,我试图将它从fragment.xml文件分配给ExpandableListView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//create adapters early
mAdapter = new DeviceAdapter(this, false);
mFilterAdapter = new DeviceAdapter(this, true);
setContentView(R.layout.activity_main);
mFragmentManager = getFragmentManager();
mNavigationDrawerFragment = (NavigationDrawerFragment)
mFragmentManager.findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
mDevices = new HashMap<String, ScanResultContent.BtLeDeviceItem>();
mFilteredDevices = new HashMap<String, ScanResultContent.BtLeDeviceItem>();
//setup fragment 1
mScanResultFragment = new ResultListFragment();
//setup fragment 2
mFilteredScanResultFragment = new ResultListFragment();
mScanResultFragment.setListAdapter(mAdapter);
//setup fragment 3
mDeviceInfoFragment = DeviceInfoFragment.newInstance("");
// Sets up UI references for the Device Info viewer.
mConnectionState = (TextView) findViewById(R.id.connection_state);
mDataField = (TextView) findViewById(R.id.data_value);
mGattServicesList = (ExpandableListView) findViewById(R.id.gatt_services_list);
mGattServicesList.setOnChildClickListener(servicesListClickListner);
mFilteredScanResultFragment.setListAdapter(mFilterAdapter);
// setup default fragment shown
mFragmentManager.beginTransaction().add(R.id.container, mScanResultFragment).commit();
}
public class DeviceInfoFragment extends Fragment {
public DeviceInfoFragment(){
super();
}
public static DeviceInfoFragment newInstance(String text) {
DeviceInfoFragment f = new DeviceInfoFragment();
Bundle b = new Bundle();
b.putString("text", text);
f.setArguments(b);
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.gatt_characteristics_layout, container, false);
return v;
}
}
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment f = null;
switch (position+1){
case 1:
f = mScanResultFragment;
mTitle = getString(R.string.title_devicescan);
break;
case 2:
f = mFilteredScanResultFragment;
mTitle = getString(R.string.title_results);
break;
case 3:
f = mDeviceInfoFragment;
mTitle = getString(R.string.title_device_info);
break;
}
if(f != null) {
mFragmentManager.beginTransaction()
.replace(R.id.container, f)
.commit();
}
restoreActionBar();
}