如何在Fragment Android上添加Activity

时间:2014-05-11 06:20:45

标签: android eclipse android-activity fragment

这是我的代码示例,我想创建一个新的应用程序,我想从我之前的项目中复制代码,但是我收到了错误。

HomeFragment.java(来自滑动菜单)

package com.example.sia;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; 

public class HomeFragment extends Fragment {

public HomeFragment(){}

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

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

    return rootView;
}
}

Account.java

package com.example.sia2;

import java.util.HashMap;

import org.json.JSONArray;
import android.os.Bundle;
import android.app.Activity;
import android.text.Html;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class Account extends Activity {

Button logout;
SessionManager session;
TextView status;
JSONArray contacts = null;
String nim, name;

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

    session = new SessionManager(getApplicationContext());
    Toast.makeText(getApplicationContext(),
            "User Login Status: " + session.isLoggedIn(), Toast.LENGTH_LONG)
            .show();

    session.checkLogin();

    HashMap<String, String> user = session.getUserDetails();

    name = user.get(SessionManager.KEY_NAME);
    // id = user.get(SessionManager.KEY_ID);

    TextView status = (TextView) findViewById(R.id.status);
    status.setText(Html.fromHtml("Welcome,<b>" + name + "</b>  "));

    logout = (Button) findViewById(R.id.logout);

    logout.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            session.logoutUser();
            finish();
        }
    });
}

}

我结合了代码

package com.example.sia;

import java.util.HashMap;

import org.json.JSONArray;

import com.example.sia.R;
import com.example.sia.SessionManager;

import android.app.Fragment;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class HomeFragment extends Fragment {

Button logout;
SessionManager session;
TextView status;
JSONArray contacts = null;
String nim, name;

public HomeFragment(){}

@Override
/**public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_account);**/

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

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



    session = new SessionManager(getActivity().getApplicationContext());
    Toast.makeText(getActivity().getApplicationContext(),"User Login Status: " + session.isLoggedIn(), Toast.LENGTH_LONG).show();

    session.checkLogin();

    HashMap<String, String> user = session.getUserDetails();

    name = user.get(SessionManager.KEY_NAME);
    // id = user.get(SessionManager.KEY_ID);

    TextView status = (TextView) getView().findViewById(R.id.status);
    status.setText(Html.fromHtml("Welcome,<b>" + name + "</b>  "));

    logout = (Button) getView().findViewById(R.id.logout);

    logout.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View arg0) 
        {
            // TODO Auto-generated method stub
            session.logoutUser();
            getActivity().finish();
        }

    });
    return rootView;  
}  


}    
/*public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

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

    return rootView;
}*/

我可以启动该应用程序,但是当我打开fragment_home.xml时,它没有响应

谢谢

2 个答案:

答案 0 :(得分:0)

在你合并之前,你应该知道片段是如何组成的,而不是活动。 (您有一个示例HomeFragment.java!)

  1. 没有setContentView方法,因为您通过覆盖onCreateView方法来扩充布局。
  2. 没有findViewById方法,而是通过调用onCreateView上的findViewByIdView找到getApplicationContext()内的引用。
  3. getActivity().getApplicationContext()可以替换为{{1}}
  4. 请在此处阅读碎片:http://developer.android.com/guide/components/fragments.html

答案 1 :(得分:0)

解决了:)谢谢你

public class HomeFragment extends Fragment {
....
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
.....
.....
}

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

    View rootView = inflater.inflate(R.layout.fragment_home, container, false);
    return rootView;
}
}