我有我的MainActivity和LoginActivity。我可以毫无问题地调用每个方法,但是当我尝试单击“btnLogin”或“btnLinkToRegister”按钮时,没有任何反应。
LoginActivity
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
import com.test.music.myapplication.MainActivity;
import com.test.music.myapplication.R;
import com.test.music.myapplication.app.LoginConfig;
import com.test.music.myapplication.app.LoginController;
import com.test.music.myapplication.helper.SQLiteHandler;
import com.test.music.myapplication.helper.SessionManager;
public class LoginActivity extends Activity{
private static final String TAG = RegisterActivity.class.getSimpleName();
private Button btnLogin;
private Button btnLinkToRegister;
private EditText inputEmail;
private EditText inputPassword;
private ProgressDialog pDialog;
private SessionManager session;
private SQLiteHandler db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_fragment);
inputEmail = (EditText) findViewById(R.id.email);
inputPassword = (EditText) findViewById(R.id.password);
btnLogin = (Button) findViewById(R.id.btnLogin);
btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
// Progress dialog
pDialog = new ProgressDialog(this);
pDialog.setCancelable(false);
// SQLite database handler
db = new SQLiteHandler(getApplicationContext());
// Session manager
session = new SessionManager(getApplicationContext());
// Check if user is already logged in or not
if (session.isLoggedIn()) {
// User is already logged in. Take him to main activity
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
// Login button Click Event
/*btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();
// Check for empty data in the form
if (!email.isEmpty() && !password.isEmpty()) {
// login user
checkLogin(email, password);
} else {
// Prompt user to enter credentials
Toast.makeText(getApplicationContext(),
"Please enter the credentials!", Toast.LENGTH_LONG)
.show();
}
}
});
// Link to Register Screen
btnLinkToRegister.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
RegisterActivity.class);
startActivity(i);
finish();
}
});*/
}
/**
* function to verify login details in mysql db
* */
public void checkLogin(final String email, final String password) {
// Tag used to cancel the request
String tag_string_req = "req_login";
pDialog.setMessage("Logging in ...");
showDialog();
StringRequest strReq = new StringRequest(Method.POST,
LoginConfig.URL_LOGIN, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Login Response: " + response.toString());
hideDialog();
try {
JSONObject jObj = new JSONObject(response);
boolean error = jObj.getBoolean("error");
// Check for error node in json
if (!error) {
// user successfully logged in
// Create login session
session.setLogin(true);
// Now store the user in SQLite
String uid = jObj.getString("uid");
JSONObject user = jObj.getJSONObject("user");
String name = user.getString("name");
String email = user.getString("email");
String created_at = user
.getString("created_at");
// Inserting row in users table
db.addUser(name, email, uid, created_at);
// Launch main activity
Intent intent = new Intent(LoginActivity.this,
MainActivity.class);
startActivity(intent);
finish();
} else {
// Error in login. Get the error message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(),
errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Login Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_LONG).show();
hideDialog();
}
}) {
@Override
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<String, String>();
params.put("email", email);
params.put("password", password);
return params;
}
};
// Adding request to request queue
LoginController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
private void showDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hideDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
public Button getBtnLogin() {
return btnLogin;
}
public Button getBtnLinkToRegister() {
return btnLinkToRegister;
}
public void setBtnLogin(Button btnLogin) {
this.btnLogin = btnLogin;
}
public void setBtnLinkToRegister(Button btnLinkToRegister) {
this.btnLinkToRegister = btnLinkToRegister;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
LoginFragment
import android.app.ProgressDialog;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.test.music.myapplication.activity.LoginActivity;
import com.test.music.myapplication.activity.RegisterActivity;
import com.test.music.myapplication.helper.SQLiteHandler;
import com.test.music.myapplication.helper.SessionManager;
public class LoginFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private Button btnLogin;
private Button btnLinkToRegister;
private EditText inputEmail;
private EditText inputPassword;
private ProgressDialog pDialog;
private SessionManager session;
private SQLiteHandler db;
private static final String TAG = RegisterActivity.class.getSimpleName();
LoginActivity Log = new LoginActivity();
private OnFragmentInteractionListener mListener;
public LoginFragment() {
// Required empty public constructor
}
public static LoginFragment newInstance(String param1, String param2) {
LoginFragment fragment = new LoginFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public void onCreate() {
//super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
// Login button Click Event
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();
if (!email.isEmpty() && !password.isEmpty()) {
Log.checkLogin(email, password);
} else {
Toast.makeText(Log.getApplicationContext(),
"Please enter the credentials!", Toast.LENGTH_LONG)
.show();
}
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.login_fragment, container, false);
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
login_fragment
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.music.myapplication.LoginFragment"
android:background="@color/bg_login"
android:gravity="center"
android:orientation="vertical"
android:padding="0dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:paddingLeft="0dp"
android:paddingRight="0dp" >
<EditText
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="@color/white"
android:hint="@string/hint_email"
android:inputType="textEmailAddress"
android:padding="10dp"
android:textColor="@color/input_login"
android:textColorHint="@color/input_login_hint" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="@color/white"
android:hint="@string/hint_password"
android:inputType="textPassword"
android:padding="10dp"
android:textColor="@color/input_login"
android:textColorHint="@color/input_login_hint" />
<!-- Login Button -->
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dip"
android:background="@color/btn_login_bg"
android:text="@string/btn_login"
android:textColor="@color/btn_login" />
<!-- Link to Login Screen -->
<Button
android:id="@+id/btnLinkToRegisterScreen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dip"
android:background="@null"
android:text="@string/btn_link_to_register"
android:textAllCaps="false"
android:textColor="@color/white"
android:textSize="15dp" />
</LinearLayout>
</FrameLayout>
logcat的
06-08 14:47:56.703 18829-18829/? I/art: Late-enabling -Xcheck:jni
06-08 14:47:56.734 18829-18829/? D/TidaProvider: TidaProvider()
06-08 14:47:56.766 18829-18829/app.myapplication W/ActivityThread: Application app.myapplication is waiting for the debugger on port 8100...
06-08 14:47:56.769 18829-18829/app.myapplication I/System.out: Sending WAIT chunk
06-08 14:47:56.958 18829-18835/app.myapplication I/art: Debugger is active
06-08 14:47:56.971 18829-18829/app.myapplication I/System.out: Debugger has connected
06-08 14:47:56.971 18829-18829/app.myapplication I/System.out: waiting for debugger to settle...
06-08 14:47:57.171 18829-18829/app.myapplication I/System.out: waiting for debugger to settle...
06-08 14:47:57.371 18829-18829/app.myapplication I/System.out: waiting for debugger to settle...
06-08 14:47:57.571 18829-18829/app.myapplication I/System.out: waiting for debugger to settle...
06-08 14:47:57.772 18829-18829/app.myapplication I/System.out: waiting for debugger to settle...
06-08 14:47:57.972 18829-18829/app.myapplication I/System.out: waiting for debugger to settle...
06-08 14:47:58.172 18829-18829/app.myapplication I/System.out: waiting for debugger to settle...
06-08 14:47:58.372 18829-18829/app.myapplication I/System.out: waiting for debugger to settle...
06-08 14:47:58.572 18829-18829/app.myapplication I/System.out: debugger has settled (1478)
06-08 14:47:58.585 18829-18829/app.myapplication W/System: ClassLoader referenced unknown path: /data/app/app.myapplication-1/lib/arm64
06-08 14:47:58.700 18829-18829/app.myapplication V/BoostFramework: mAcquireFunc method = public int com.qualcomm.qti.Performance.perfLockAcquire(int,int[])
06-08 14:47:58.700 18829-18829/app.myapplication V/BoostFramework: mReleaseFunc method = public int com.qualcomm.qti.Performance.perfLockRelease()
06-08 14:47:58.700 18829-18829/app.myapplication V/BoostFramework: mAcquireTouchFunc method = public int com.qualcomm.qti.Performance.perfLockAcquireTouch(android.view.MotionEvent,android.util.DisplayMetrics,int,int[])
06-08 14:47:58.700 18829-18829/app.myapplication V/BoostFramework: mIOPStart method = public int com.qualcomm.qti.Performance.perfIOPrefetchStart(int,java.lang.String)
06-08 14:47:58.700 18829-18829/app.myapplication V/BoostFramework: mIOPStop method = public int com.qualcomm.qti.Performance.perfIOPrefetchStop()
06-08 14:47:58.704 18829-18829/app.myapplication V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@f8b3883
06-08 14:47:58.704 18829-18829/app.myapplication V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@1d12a00
06-08 14:47:58.704 18829-18829/app.myapplication V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@cdb1239
06-08 14:47:58.704 18829-18829/app.myapplication V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@9ea507e
06-08 14:47:58.803 18829-18829/app.myapplication V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@11983e3
06-08 14:47:58.803 18829-18829/app.myapplication V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@29e81e0
06-08 14:47:58.835 18829-18829/app.myapplication D/SQLiteHandler: Fetching user from Sqlite: {}
06-08 14:47:58.889 18829-18971/app.myapplication D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-08 14:47:58.910 18829-18829/app.myapplication D/ActivityThreadInjector: clearCachedDrawables.
06-08 14:47:58.996 18829-18971/app.myapplication I/Adreno: QUALCOMM build : a7823f5, I59a6815413
Build Date : 09/23/16
OpenGL ES Shader Compiler Version: XE031.07.00.00
Local Branch : mybranch22028469
Remote Branch : quic/LA.BR.1.3.3_rb2.26
Remote Branch : NONE
Reconstruct Branch : NOTHING
06-08 14:47:59.030 18829-18971/app.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
06-08 14:47:59.072 18829-18971/app.myapplication E/HAL: hw_get_module_by_class: module name gralloc
06-08 14:47:59.072 18829-18971/app.myapplication E/HAL: hw_get_module_by_class: module name gralloc
06-08 14:48:02.796 18829-18829/app.myapplication W/PathParser: Points are too far apart 4.000000596046461
06-08 14:48:03.830 18829-18829/app.myapplication W/PathParser: Points are too far apart 4.000000596046461
如果您需要更多代码,请告诉我们。
答案 0 :(得分:1)
首先,永远不要这样做......在活动上调用new
LoginActivity Log = new LoginActivity()
其次,你确定你在使用Fragments吗?
这是一项活动
LoginActivity extends Activity
您只是将Fragment布局放入Activity
setContentView(R.layout.login_fragment);
你似乎从未真正使用过LoginFragment类
我认为您加载了错误的类,因为如果您在此活动代码上使用了startActivity
,那么您的按钮就可以正常工作
答案 1 :(得分:1)
您编写编辑您的片段类
public class LoginFragment extends Fragment {
public Button btnLogin;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.login_fragment,container,false):
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String email = inputEmail.getText().toString().trim();
String password = inputPassword.getText().toString().trim();
// Check for empty data in the form
if (!email.isEmpty() && !password.isEmpty()) {
// login user
checkLogin(email, password);
} else {
// Prompt user to enter credentials
Toast.makeText(getApplicationContext(),
"Please enter the credentials!", Toast.LENGTH_LONG)
.show();
}
}
});
// Link to Register Screen
btnLinkToRegister.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(),
RegisterActivity.class);
startActivity(i);
finish();
}
});
return view;
}
}
写这种方式在登录片段.xml
中帮助太多了