Java 无法从活动转换为片段

Java 无法从活动转换为片段,java,android,android-fragments,android-activity,Java,Android,Android Fragments,Android Activity,我试图将我的活动类转换为片段类,因为我希望我的活动页面在片段页面上运行。这是我更改任何内容之前的活动课: public class People extends Activity { ImageButton img_btn_save, img_btn_load, img_btn_edit, img_btn_delete; EditText et_name, et_amount, et_description; ListView lv_people_info; String name, amo

我试图将我的活动类转换为片段类,因为我希望我的活动页面在片段页面上运行。这是我更改任何内容之前的活动课:

public class People extends Activity {

ImageButton img_btn_save, img_btn_load, img_btn_edit, img_btn_delete; 
EditText et_name, et_amount, et_description;
ListView lv_people_info;
String name, amount, description;
OwesomeDBHelper mOwesomeDBHelper;
SQLiteDatabase db;

private String[] allColumns = { PeopleEntry._ID,
        PeopleEntry.COLUMN_NAME, PeopleEntry.COLUMN_AMOUNT, PeopleEntry.COLUMN_DESCRIPTION};

ArrayList<String> people_info;
ArrayList<String> people_info2;

String selection_delete_where = PeopleEntry.COLUMN_NAME + " LIKE ?";

String[] selectionArgs_delete_where;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_people);

    img_btn_save = (ImageButton) findViewById(R.id.img_btn_people_save);
    img_btn_load = (ImageButton) findViewById(R.id.img_btn_people_load);
    img_btn_edit = (ImageButton) findViewById(R.id.img_btn_people_edit);
    img_btn_delete = (ImageButton) findViewById(R.id.img_btn_people_delete);

    et_name = (EditText) findViewById(R.id.et_people_name);
    et_amount = (EditText) findViewById(R.id.et_people_amount);
    et_description = (EditText) findViewById(R.id.et_people_description);

    lv_people_info =(ListView) findViewById(R.id.lv_people);

    mOwesomeDBHelper = new OwesomeDBHelper(this);
    db = mOwesomeDBHelper.getWritableDatabase();

    refresh();

    img_btn_save.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            name = et_name.getText().toString();
            amount = et_amount.getText().toString();
            description = et_description.getText().toString();

            ContentValues values = new ContentValues();
            values.put(PeopleEntry.COLUMN_NAME, name);
            values.put(PeopleEntry.COLUMN_AMOUNT, amount);
            values.put(PeopleEntry.COLUMN_DESCRIPTION, description);

            insertData (db, values);

            et_name.setText(null);
            et_amount.setText(null);
            et_description.setText(null);

            refresh();
        }
    });

    img_btn_edit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            ContentValues values = new ContentValues();
            values.put(PeopleEntry.COLUMN_AMOUNT, et_amount.getText().toString());
            values.put(PeopleEntry.COLUMN_DESCRIPTION, et_description.getText().toString());

            String selection_update = PeopleEntry.COLUMN_NAME + " LIKE ?";
            String[] selectionArgs_update = new String[] { et_name.getText().toString() };

            int count = db.update(PeopleEntry.TABLE_NAME, values,
                    selection_update, selectionArgs_update);

            Message.message(getApplicationContext(), count + ": Record Updated");

            refresh();  
        }
    });


    img_btn_delete.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub  
            selectionArgs_delete_where = new String[] { et_name.getText().toString()};

            db.delete(PeopleEntry.TABLE_NAME,
                    selection_delete_where,
                    selectionArgs_delete_where);

            et_name.setText(null);
            et_amount.setText(null);
            et_description.setText(null);

            Message.message(getApplicationContext(), "Record Deleted"); 
            refresh();
        }
    });


}

public void refresh(){
    people_info = new ArrayList<String>();

    Cursor cursor = db.query(PeopleEntry.TABLE_NAME,// Table
            allColumns, // The columns to return
            null, // The columns for the WHERE clause
            null, // The values for the WHERE clause
            null, // don't group the rows
            null, // don't filter by row groups
            null, // The sort order
            null); // Limits the number of rows returned by the  query  

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        people_info.add(" \n" + cursor.getString(0) + ". Name: "
            + cursor.getString(1) + " \n     Amount (RM): "
            + cursor.getString(2) + " \n     Description: "
            + cursor.getString(3) + " \n");
            cursor.moveToNext();
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            getApplicationContext(),
            android.R.layout.simple_list_item_1, people_info);

    lv_people_info.setAdapter(adapter);
        cursor.close();
}

public void insertData(SQLiteDatabase db, ContentValues values) {
    long newRowId;
    newRowId = db.insert(PeopleEntry.TABLE_NAME, null, values);

    if (newRowId != -1) {
        Message.message(this, "New Data Inserted");
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.people, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}}
公共类人员扩展活动{
图像按钮img\u btn\u保存、img\u btn\u加载、img\u btn\u编辑、img\u btn\u删除;
编辑文本et_名称、et_金额、et_说明;
ListView lv_人员信息;
字符串名称、金额、说明;
OwesomeDBHelper;
sqlitedb数据库;
私有字符串[]allColumns={PeopleEntry.\u ID,
PeopleEntry.COLUMN\u NAME,PeopleEntry.COLUMN\u AMOUNT,PeopleEntry.COLUMN\u DESCRIPTION};
arraylistpeople\u info;
ArrayList people_info2;
字符串选择_delete_其中=PeopleEntry.COLUMN_NAME+“LIKE?”;
字符串[]selectionArgs\u delete\u其中;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_-people);
img_btn_save=(ImageButton)findViewById(R.id.img_btn_people_save);
img_btn_load=(ImageButton)findViewById(R.id.img_btn_people_load);
img_btn_edit=(ImageButton)findViewById(R.id.img_btn_people_edit);
img_btn_delete=(ImageButton)findViewById(R.id.img_btn_people_delete);
et_name=(EditText)findViewById(R.id.et_people_name);
et金额=(EditText)findViewById(R.id.et金额);
et_description=(EditText)findViewById(R.id.et_people_description);
lv_people_info=(ListView)findViewById(R.id.lv_people);
mOwesomeDBHelper=新的OwesomeDBHelper(此);
db=mOwesomeDBHelper.getWritableDatabase();
刷新();
img_btn_save.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
name=et_name.getText().toString();
amount=et_amount.getText().toString();
description=et_description.getText().toString();
ContentValues=新的ContentValues();
value.put(PeopleEntry.COLUMN\u NAME,NAME);
value.put(PeopleEntry.COLUMN\u AMOUNT,AMOUNT);
value.put(PeopleEntry.COLUMN_DESCRIPTION,DESCRIPTION);
插入数据(db,值);
et_name.setText(空);
et_amount.setText(空);
et_description.setText(空);
刷新();
}
});
img_btn_edit.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
ContentValues=新的ContentValues();
value.put(PeopleEntry.COLUMN_AMOUNT,et_AMOUNT.getText().toString());
value.put(PeopleEntry.COLUMN_DESCRIPTION,et_DESCRIPTION.getText().toString());
字符串选择_update=PeopleEntry.COLUMN_NAME+“LIKE?”;
String[]selectionArgs_update=新字符串[]{et_name.getText().toString()};
int count=db.update(PeopleEntry.TABLE_名称、值、,
选择更新,选择更新);
Message.Message(getApplicationContext(),count+“:记录已更新”);
刷新();
}
});
img_btn_delete.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
selectionArgs_delete_其中=新字符串[]{et_name.getText().toString()};
db.delete(PeopleEntry.TABLE_NAME,
选择_删除_其中,
选择GS(删除位置);
et_name.setText(空);
et_amount.setText(空);
et_description.setText(空);
Message.Message(getApplicationContext(),“记录已删除”);
刷新();
}
});
}
公共无效刷新(){
people_info=new ArrayList();
Cursor Cursor=db.query(PeopleEntry.TABLE_NAME,//TABLE
allColumns,//要返回的列
null,//WHERE子句的列
null,//WHERE子句的值
null,//不对行进行分组
null,//不按行组筛选
null,//排序顺序
null);//限制查询返回的行数
cursor.moveToFirst();
而(!cursor.isAfterLast()){
人员信息。添加(“\n”+游标。getString(0)+”。名称:
+cursor.getString(1)+“\n金额(RM):”
+cursor.getString(2)+“\n说明:”
+cursor.getString(3)+“\n”);
cursor.moveToNext();
}
ArrayAdapter适配器=新的ArrayAdapter(
getApplicationContext(),
android.R.layout.simple_list_item_1,people_info);
lv_people_info.setAdapter(适配器);
cursor.close();
}
公共void insertData(SQLiteDatabase db,ContentValues){
长纽罗维德;
newRowId=db.insert(PeopleEntry.TABLE_NAME,null,value);
如果(newRowId!=-1){
消息。消息(本“插入的新数据”);
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(R.menu.people,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
int id=item.getItemId();
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}}
代码运行得很好,但是,当我尝试将其转换为片段时,似乎出现了错误。这是我尝试的片段代码:

public class People extends Fragment {

ImageButton img_btn_save, img_btn_load, img_btn_edit, img_btn_delete; 
EditText et_name, et_amount, et_description;
ListView lv_people_info;
String name, amount, description;
OwesomeDBHelper mOwesomeDBHelper;
SQLiteDatabase db;

//Store columns in array
private String[] allColumns = { PeopleEntry._ID,
        PeopleEntry.COLUMN_NAME, PeopleEntry.COLUMN_AMOUNT, PeopleEntry.COLUMN_DESCRIPTION};

//Declare array list for list view
ArrayList<String> people_info;
ArrayList<String> people_info2;

// Define 'where' part of query.
String selection_delete_where = PeopleEntry.COLUMN_NAME + " LIKE ?";

// Specify arguments in placeholder order.
String[] selectionArgs_delete_where;

public People(){}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_people, container, false);

    FragmentActivity    faActivity  = (FragmentActivity)    super.getActivity();

 // Replace LinearLayout by the type of the root element of the layout you're trying to load
    LinearLayout        llLayout    = (LinearLayout)    inflater.inflate(R.layout.fragment_people, container, false);

    img_btn_save = (ImageButton) llLayout.findViewById(R.id.img_btn_people_save);
    img_btn_load = (ImageButton) llLayout.findViewById(R.id.img_btn_people_load);
    img_btn_edit = (ImageButton) llLayout.findViewById(R.id.img_btn_people_edit);
    img_btn_delete = (ImageButton) llLayout.findViewById(R.id.img_btn_people_delete);

    et_name = (EditText) llLayout.findViewById(R.id.et_people_name);
    et_amount = (EditText) llLayout.findViewById(R.id.et_people_amount);
    et_description = (EditText) llLayout.findViewById(R.id.et_people_description);

    lv_people_info =(ListView) llLayout.findViewById(R.id.lv_people);

    mOwesomeDBHelper = new OwesomeDBHelper(super.getActivity());
    db = mOwesomeDBHelper.getWritableDatabase();

    refresh();

    img_btn_save.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            name = et_name.getText().toString();
            amount = et_amount.getText().toString();
            description = et_description.getText().toString();

            ContentValues values = new ContentValues();
            values.put(PeopleEntry.COLUMN_NAME, name);
            values.put(PeopleEntry.COLUMN_AMOUNT, amount);
            values.put(PeopleEntry.COLUMN_DESCRIPTION, description);

            insertData (db, values);

            et_name.setText(null);
            et_amount.setText(null);
            et_description.setText(null);

            refresh();
        }
    });

    img_btn_edit.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            ContentValues values = new ContentValues();
            values.put(PeopleEntry.COLUMN_AMOUNT, et_amount.getText().toString());
            values.put(PeopleEntry.COLUMN_DESCRIPTION, et_description.getText().toString());
            // Which row to update, based on the name
            String selection_update = PeopleEntry.COLUMN_NAME + " LIKE ?";
            String[] selectionArgs_update = new String[] { et_name.getText().toString() };

            int count = db.update(PeopleEntry.TABLE_NAME, values,
                    selection_update, selectionArgs_update);

            Message.message(getActivity().getApplicationContext(), count + ": Record Updated");

            refresh();  
        }
    });


    img_btn_delete.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub  
            selectionArgs_delete_where = new String[] { et_name.getText().toString()};

            db.delete(PeopleEntry.TABLE_NAME,
                    selection_delete_where,
                    selectionArgs_delete_where);

            et_name.setText(null);
            et_amount.setText(null);
            et_description.setText(null);

            Message.message(getActivity().getApplicationContext(), "Record Deleted");   
            refresh();
        }           
    });

    //// I get error on either one of these:
    return rootView;
    return llLayout;

}

public void refresh(){
    people_info = new ArrayList<String>();

    Cursor cursor = db.query(PeopleEntry.TABLE_NAME,// Table
            allColumns, // The columns to return
            null, // The columns for the WHERE clause
            null, // The values for the WHERE clause
            null, // don't group the rows
            null, // don't filter by row groups
            null, // The sort order
            null); // Limits the number of rows returned by the  query  

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
        people_info.add(" \n" + cursor.getString(0) + ". Name: "
            + cursor.getString(1) + " \n     Amount (RM): "
            + cursor.getString(2) + " \n     Description: "
            + cursor.getString(3) + " \n");
            cursor.moveToNext();
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            getActivity().getApplicationContext(),
            android.R.layout.simple_list_item_1, people_info);

    lv_people_info.setAdapter(adapter);
        cursor.close();
}

public void insertData(SQLiteDatabase db, ContentValues values) {
    long newRowId;
    newRowId = db.insert(PeopleEntry.TABLE_NAME, null, values);

    if (newRowId != -1) {
        Message.message(super.getActivity(), "New Data Inserted");
    }

}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    /////// I suddenly get red underline on "people"
    getActivity().getMenuInflater().inflate(R.menu.people, menu);
    //return true;
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}}
公共类人员扩展片段{
图像按钮img\u btn\u保存、img\u btn\u加载、img\u btn\u编辑、img\u btn\u删除;
编辑文本et_名称、et_金额、et_说明;
ListView lv_人员信息;
字符串名称、金额、说明;
OwesomeDBHelper;
sqlitedb数据库;
//在数组中存储列
私有字符串[]allColumns={PeopleEntry.\u ID,
PeopleEntry.COLUMN\u NAME,PeopleEntry.COLUMN\u AMOUNT,PeopleEntry.COLUMN\u DESCRIPTION};
//声明数组li
LinearLayout llLayout = (LinearLayout)inflater.inflate(R.layout.fragment_people, container, false);
 View rootView = inflater.inflate(R.layout.fragment_people, container, false);
 LinearLayout llLayout = (LinearLayout)inflater.inflate(R.layout.fragment_people, container, false);
 return llLayout;
getActivity().getMenuInflater().inflate(R.menu.people, menu);
inflater.inflate(R.menu.people, menu);