如何在 Fragment Android 上添加 Activity

问题描述 投票:0回答:2

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

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;
}
}

账户.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
时,它没有响应

谢谢

android eclipse android-activity fragment
2个回答
0
投票

在你开始合并之前,你可能应该知道片段是如何组成的,而不是一个活动。 (你有一个样品

HomeFragment.java
!)

  1. 没有
    setContentView
    方法,因为您通过覆盖
    onCreateView
    方法来膨胀布局。
  2. 没有
    findViewById
    方法,而是通过在您膨胀的
    onCreateView
    上调用
    findViewById
    View
    中找到那些引用。
  3. getApplicationContext()
    可以替换为
    getActivity().getApplicationContext()

请在此处阅读 Fragments:http://developer.android.com/guide/components/fragments.html


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;
}
}
© www.soinside.com 2019 - 2024. All rights reserved.