Android 如何将简单活动类分解成片段?
我有两个java文件,一个扩展了简单活动MainActivity.java,另一个扩展了片段sample.java,当我试图在MainActivity中设置第二个时,它显示错误。ActivityNotFoundException:无法找到显式活动类 公共类LoginPage扩展活动实现OnClickListener{Android 如何将简单活动类分解成片段?,android,Android,我有两个java文件,一个扩展了简单活动MainActivity.java,另一个扩展了片段sample.java,当我试图在MainActivity中设置第二个时,它显示错误。ActivityNotFoundException:无法找到显式活动类 公共类LoginPage扩展活动实现OnClickListener{ private Button btn; private EditText user; private EditText pass; // Progress Dialog priv
private Button btn;
private EditText user;
private EditText pass;
// Progress Dialog
private ProgressDialog pDialog;
//JSON parser class
JSONParser jsonParser = new JSONParser();
private Button btn1;
private static final String LOGIN_URL = "";
private static final String TAG_SUCCESS = "status";
private static final String TAG_LOGIN = "login";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.login_page);
user=(EditText)findViewById(R.id.loginmailid);
pass=(EditText)findViewById(R.id.loginpwd);
btn=(Button)findViewById(R.id.login);
btn1=(Button)findViewById(R.id.btnreg);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.login:
new AttemptLogin().execute();
break;
case R.id.btnreg:
Intent i = new Intent(this, RegistrationForm.class);
startActivity(i);
break;
default:
break;
}
}
类AttemptLogin扩展了AsyncTask{
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(LoginPage.this);
pDialog.setMessage("Login..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@SuppressWarnings("unused")
@Override
protected String doInBackground(String...args) {
//Check for success tag
//int success;
Looper.prepare();
String username = user.getText().toString();
String password = pass.getText().toString();
try {
//Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("email", username));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("version", "apps"));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest (
LOGIN_URL, "POST", params);
//check your log for json response
Log.d("Login attempt", json.toString());
JSONObject jobj = new JSONObject(json.toString());
final String msg = jobj.getString("msg");
System.out.println("MSG : " + msg);
runOnUiThread(new Runnable()
{
@Override
public void run()
{
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
});
return json.getString(TAG_SUCCESS);
//http://gujjumatch.com/login?version=apps&email=GM847903@param.in&password=123456
//JSONArray arr = json.getJSONArray("login");
//System.out.println(arr.toString());
//JSONObject arr1 = new JSONObject(json);
//String ss=arr1.getString("status");
//System.out.println(ss);
//System.out.println(arr1.getString("status"));
//String date = jObj.getString("status");
// json success tag
// success = json.getInt(TAG_SUCCESS);
}catch (JSONException e) {
e.printStackTrace();
}
return null;
}
// After completing background task Dismiss the progress dialog
protected void onPostExecute(String file_url) {
//dismiss the dialog once product deleted
pDialog.dismiss();
if(file_url.equals("success")) {
// Log.d("Login Successful!", json.toString());
Intent i = new Intent(LoginPage.this, HomeFragment.class);
startActivity(i);
Toast.makeText(getApplicationContext(), "Login Successful", Toast.LENGTH_LONG).show();
}else{
//Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG).show();
}
}}
boolean故障=false;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=新建进度对话框(LoginPage.this);
pDialog.setMessage(“登录…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
@抑制警告(“未使用”)
@凌驾
受保护的字符串doInBackground(字符串…args){
//检查成功标签
//成功;
Looper.prepare();
字符串username=user.getText().toString();
字符串密码=pass.getText().toString();
试一试{
//建筑参数
List params=new ArrayList();
添加(新的BasicNameValuePair(“电子邮件”,用户名));
添加(新的BasicNameValuePair(“密码”,password));
参数添加(新的BasicNameValuePair(“版本”、“应用程序”);
Log.d(“请求!”,“启动”);
//通过发出HTTP请求获取产品详细信息
JSONObject json=jsonParser.makeHttpRequest(
登录URL,“POST”,参数);
//检查日志中的json响应
Log.d(“登录尝试”,json.toString());
JSONObject jobj=新的JSONObject(json.toString());
最终字符串msg=jobj.getString(“msg”);
System.out.println(“MSG:+MSG”);
runOnUiThread(新的Runnable()
{
@凌驾
公开募捐
{
Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_LONG.show();
}
});
返回json.getString(TAG_SUCCESS);
//http://gujjumatch.com/login?version=apps&email=GM847903@参数输入和密码=123456
//JSONArray arr=json.getJSONArray(“登录”);
//System.out.println(arr.toString());
//JSONObject arr1=新的JSONObject(json);
//字符串ss=arr1.getString(“状态”);
//系统输出打印LN(ss);
//System.out.println(arr1.getString(“status”);
//字符串日期=jObj.getString(“状态”);
//json成功标记
//success=json.getInt(TAG_success);
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
//完成后台任务后,关闭“进度”对话框
受保护的void onPostExecute(字符串文件\u url){
//删除产品后关闭对话框
pDialog.disclose();
if(file_url.equals(“success”)){
//Log.d(“登录成功!”,json.toString();
Intent i=新Intent(LoginPage.this,HomeFragment.class);
星触觉(i);
Toast.makeText(getApplicationContext(),“登录成功”,Toast.LENGTH_LONG.show();
}否则{
//Toast.makeText(getApplicationContext(),“Failed”,Toast.LENGTH_LONG.show();
}
}}
}因为它是一个片段。这不是一项活动。请将其放入活动中,然后重试源代码。你是如何使用intent从activity调用fragment的?很明显,因为你的第二个java类是
fragment
而不是activity
,它从不使用intent
调用。你知道如何打开fragmantin吗android@Eddie参考这些链接,我也遇到过同样的问题,请尝试此链接,它将提供帮助我正在使用导航抽屉,无法将其置于活动中。因此,请使用FrameLayout并在需要时替换片段