我试图在我的应用上使用Fragments,但由于某种原因我得到了这个错误"无法解决方法' add(int,com。 ... .LoginFragment) 。我不知道该怎么办。我havce试图导入android.app.v4 ....但它仍然给我这个错误。
这是代码
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
public class TemplateLogReg extends Activity implements Connector {
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
private static final String TAG = "Login";
LoginFragment lg;
EditText userTxt, passTxt;
Button ret, reg, loginBtn, btnNewUser;
private RegisterUser regUser;
EditText phone, email, password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.template_log_reg);
if (checkPlayServices()) {
// Start IntentService to register this application with GCM.
Intent intent = new Intent(this, RegistrationIntentService.class);
startService(intent);
}
reg = (Button) findViewById(R.id.regButton);
phone = (EditText) findViewById(R.id.phoneEt);
email = (EditText) findViewById(R.id.emailEt);
password = (EditText) findViewById(R.id.passwordEt);
ret = (Button) findViewById(R.id.ret);
btnNewUser = (Button) findViewById(R.id.btnNewUser);
userTxt = (EditText) findViewById(R.id.etPhone);
passTxt = (EditText) findViewById(R.id.etPassword);
loginBtn = (Button) findViewById(R.id.loginBtn);
lg = new LoginFragment();
lg.setInterface(this);
/** this code show us the login page. 5 lines **/
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
lg = new LoginFragment();
lg.setInterface(this);
ft.add(R.id.fragment_container, lg); // this line error
ft.commit();
答案 0 :(得分:4)
您正在导入错误的类
替换
import android.app.Fragment;
import android.app.FragmentManager
import android.app.FragmentTransaction;
使用他们的v4版本
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentTransaction;
并确保LoginFragment
本身正在导入android.support.v4.app.Fragment
。
然后,您可能也应该不使用基础Activity
类。
public class TemplateLogReg extends FragmentActivity
或者,如果您使用的是appcompat-v7库,那么
public class TemplateLogReg extends AppCompatActivity
我试过
import android.app.v4....
,但它仍然给我这个错误。
要使用v4 FragmentManager
课程,请将getFragmentManager()
替换为getSupportFragmentManager()