这是我的代码示例,我想做一个新的应用程序,我想从我以前的项目中复制代码,但我得到了一个错误。
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条答案
按热度按时间siv3szwd1#
在进行合并之前,您可能应该了解片段相对于活动是如何组成的。(您有一个示例
HomeFragment.java
!)1.没有
setContentView
方法,因为您通过覆盖onCreateView
方法来膨胀布局。1.没有
findViewById
方法,而是通过在您膨胀的View
上调用findViewById
来在onCreateView
中找到这些引用。1.
getApplicationContext()
可替换为getActivity().getApplicationContext()
请阅读此处的片段:http://developer.android.com/guide/components/fragments.html
kd3sttzy2#
已解决:)谢谢