我正在我的应用中实现导航抽屉,并且代码如下
MainActivity.java
import android.support.v4.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import com.example.project.project.TimeSheet.ObjectDrawerItem;
public class MainActivity extends AppCompatActivity {
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get list items from strings.xml
mNavigationDrawerItemTitles = getResources().getStringArray(R.array.nav_drawer_items);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// 2.1 create ActionBarDrawerToggle
ObjectDrawerItem[] drawerItem = new ObjectDrawerItem[3];
drawerItem[0] = new ObjectDrawerItem(R.mipmap.timesheet, "Time Sheet");
drawerItem[1] = new ObjectDrawerItem(R.mipmap.claims, "Claims");
DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.listview_item_row, drawerItem);
mDrawerList.setAdapter(adapter);
}
public class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
private void selectItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment=new Information();
break;
case 1:
fragment = new Claims();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
getActionBar().setTitle(mNavigationDrawerItemTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
}
}
我在fragment
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
当我指向fragment
时,我得到了
Wrong 2nd ArgumentType.Found 'android.support.v4.app.Fragment', required 'android.app.Fragment';
这是我的信息活动的一部分
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import com.example.project.project.database.MyDatabaseHelper;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class Information extends Fragment implements View.OnClickListener {
private Spinner spinner;
private MyDatabaseHelper dbHelper;
private com.example.project.project.API.InfoAPI ts;
private static EditText txtDate;
private DateDialog dialog;
private static String a;
private static String date1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View info = inflater.inflate(R.layout.information, container, false);
dialog=new DateDialog();
ts = new com.example.project.project.API.InfoAPI(getActivity());
txtDate = (EditText) info.findViewById(R.id.editText5);
spinner = (Spinner) info.findViewById(R.id.spinner);
txtDate.setOnClickListener(this);
addItemsOnSpinner();
btnSaved.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
String date=txtDate.getText().toString();
ts.insertTimeSheet(spinner.getSelectedItem().toString(),spinner2.getSelectedItem().toString(),date,spinner3.getSelectedItem().toString());
Toast.makeText(getApplicationContext(), "Saved", Toast.LENGTH_SHORT).show();
}
});
Button button=(Button)info.findViewById(R.id.button5);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent(getActivity().getApplicationContext(), WorkForce.class);
a = spinner.getSelectedItem().toString();
intent.putExtra("a",a);
startActivity(intent);
}
});
return info;
}
public void onClick(View arg0) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
dialog.show(ft, "DatePicker");
}
public void addItemsOnSpinner() {
List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
LogCat错误
Error:(86, 80) error: incompatible types: android.support.v4.app.Fragment cannot be converted to android.app.Fragment
如果我import android.app.Fragment
同时参与这两项活动,我会在我的Information.java
public void onClick(View arg0) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
dialog.show(ft, "DatePicker");
}
答案 0 :(得分:3)
更改
getFragmentManager()
到
getSupportFragmentManager()