java.lang.NoSuchMethodError:没有虚方法setTag(Ljava / lang / Object;)

时间:2017-06-05 14:01:27

标签: java android mysql android-volley

我刚刚完成了使用登录和注册屏幕与volley

的代码

但是在模拟器中运行时出现此错误

java.lang.NoSuchMethodError: No virtual method setTag(Ljava/lang/Object;)Lcom/android/volley/Request; in class Lcom/android/volley/Request; or its super classes (declaration of 'com.android.volley.Request' appears in /data/app/com.dreacot.street_crime-2/split_lib_dependencies_apk.apk:classes10.dex)

根据调试,这些是有问题的文件

请有人告诉我我做错了吗?

AppController.java

public class AppController extends Application {

public static final String TAG = AppController.class.getSimpleName();

private RequestQueue mRequestQueue;

private static AppController mInstance;

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
}

public static synchronized AppController getInstance() {
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }

    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

public <T> void addToRequestQueue(Request<T> req) {
    req.setTag(TAG);
    getRequestQueue().add(req);
}

public void cancelPendingRequests(Object tag) {
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(tag);
    }
}
}

SignUpActivity.java

public class SignupActivity extends Activity {

private static final String TAG = SignupActivity.class.getSimpleName();
Button regbutton;
private EditText inputFullName;
private EditText inputEmail;
private EditText inputPassword;
private SessionManager session;
private SQLiteHandler db;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Set View to register.xml
    setContentView(R.layout.activity_register);

    inputFullName = (EditText) findViewById(R.id.reg_username);
    inputEmail = (EditText) findViewById(R.id.reg_email);
    inputPassword = (EditText) findViewById(R.id.reg_password);

    // Session manager
    session = new SessionManager(getApplicationContext());

    // SQLite database handler
    db = new SQLiteHandler(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(SignupActivity.this,
                AfterLogin.class);
        startActivity(intent);
        finish();
    }

    addListenerOnButton();

    TextView loginScreen = (TextView) findViewById(R.id.link_to_login);

    // Listening to Login Screen link
    loginScreen.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // Closing registration screen
            // Switching to Login Screen/closing register screen
            finish();
        }
    });

}

public void addListenerOnButton() {

    regbutton = (Button) findViewById(R.id.btnRegister);

    regbutton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            String name = inputFullName.getText().toString().trim();
            String email = inputEmail.getText().toString().trim();
            String password = inputPassword.getText().toString().trim();

            if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
                registerUser(name, email, password);
            } else {
                Toast.makeText(getApplicationContext(),
                        "Please enter your details!", Toast.LENGTH_LONG)
                        .show();
            }
        }
    });
}
/**
 * Function to store user in MySQL database will post params(tag, name,
 * email, password) to register url
 * */
private void registerUser(final String name, final String email,
                          final String password) {
    // Tag used to cancel the request
    String tag_string_req = "req_register";

    StringRequest strReq = new StringRequest(Method.POST,
            AppConfig.URL_REGISTER, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Register Response: " + response.toString());

            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");
                if (!error) {
                    // User successfully stored in MySQL
                    // 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);

                    Toast.makeText(getApplicationContext(), "User successfully registered. Try login now!", Toast.LENGTH_LONG).show();

                    // Launch login activity
                    Intent intent = new Intent(
                            SignupActivity.this,
                            LoginActivity.class);
                    startActivity(intent);
                    finish();
                } else {

                    // Error occurred in registration. Get the error
                    // message
                    String errorMsg = jObj.getString("error_msg");
                    Toast.makeText(getApplicationContext(),
                            errorMsg, Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Registration Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            // Posting params to register url
            Map<String, String> params = new HashMap<String, String>();
            params.put("name", name);
            params.put("email", email);
            params.put("password", password);

            return params;
        }

    };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}
}

日志

06-05 15:38:27.457 28919-28919/com.dreacot.street_crime E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: com.dreacot.street_crime, PID: 28919
                                                                      java.lang.NoSuchMethodError: No virtual method setTag(Ljava/lang/Object;)Lcom/android/volley/Request; in class Lcom/android/volley/Request; or its super classes (declaration of 'com.android.volley.Request' appears in /data/app/com.dreacot.street_crime-2/split_lib_dependencies_apk.apk:classes10.dex)
                                                                          at com.dreacot.street_crime.app.AppController.addToRequestQueue(AppController.java:37)
                                                                          at com.dreacot.street_crime.other.SignupActivity.registerUser(SignupActivity.java:181)
                                                                          at com.dreacot.street_crime.other.SignupActivity.access$300(SignupActivity.java:31)
                                                                          at com.dreacot.street_crime.other.SignupActivity$2.onClick(SignupActivity.java:94)
                                                                          at android.view.View.performClick(View.java:5610)
                                                                          at android.view.View.onKeyUp(View.java:10581)
                                                                          at android.widget.TextView.onKeyUp(TextView.java:6435)
                                                                          at android.view.KeyEvent.dispatch(KeyEvent.java:2700)
                                                                          at android.view.View.dispatchKeyEvent(View.java:9880)
                                                                          at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1667)
                                                                          at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1667)
                                                                          at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1667)
                                                                          at android.widget.ScrollView.dispatchKeyEvent(ScrollView.java:379)
                                                                          at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1667)
                                                                          at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1667)
                                                                          at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1667)
                                                                          at com.android.internal.policy.DecorView.superDispatchKeyEvent(DecorView.java:403)
                                                                          at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1800)
                                                                          at android.app.Activity.dispatchKeyEvent(Activity.java:3021)
                                                                          at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:317)
                                                                          at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4327)
                                                                          at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4298)
                                                                          at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
                                                                          at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
                                                                          at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
                                                                          at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3995)
                                                                          at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
                                                                          at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4052)
                                                                          at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
                                                                          at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3902)
                                                                          at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3868)
                                                                          at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3876)
                                                                          at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3849)
                                                                          at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6210)
                                                                          at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6184)
                                                                          at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6145)
                                                                          at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3647)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                          at android.os.Looper.loop(Looper.java:154)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

1 个答案:

答案 0 :(得分:2)

我的gradle依赖项中有两个Volley依赖项,所以我猜那是什么原因导致了这一切

compile 'com.mcxiaoke.volley:library:1.0.19'
compile 'com.mcxiaoke.volley:library-aar:1.0.0'

我刚拿出这个

compile 'com.mcxiaoke.volley:library:1.0.19'

现在一切正常