如何在Fragment Android上添加活动

xlpyo6sf  于 2022-09-21  发布在  Android
关注(0)|答案(2)|浏览(179)

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

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

谢谢

siv3szwd

siv3szwd1#

在进行合并之前,您可能应该了解片段相对于活动是如何组成的。(您有一个示例HomeFragment.java!)

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

请阅读此处的片段:http://developer.android.com/guide/components/fragments.html

kd3sttzy

kd3sttzy2#

已解决:)谢谢

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

相关问题