jmartinesp / SwissKnife

A multi-purpose library containing view injection and threading for Android using annotations
Apache License 2.0
254 stars 24 forks source link

SwissKnife not working in Activity (except MainActivity) #18

Closed newjing closed 9 years ago

newjing commented 9 years ago

I've been using SwissKnife with MainActivity, and everything is working fine.

However, when I create a second Activity, swissknife cannot work.

My second activity code as below (With a Button "testBtn" in layout)

public class KFLoginActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kflogin);

        //SwissKnife.inject(this);           //       #1   Compile Error
        SwissKnife.restoreState(this, savedInstanceState);
    }
    @OnClick(R.id.testBtn)
    def testIt(){
        Log.e("TEST","work withouit swissknife.inject")         //   #2 doesn't work
    }
}

Problem 1: If I input SwissKnife.inject(this) , Compile Error as below:

 Caused by: groovy.lang.MissingMethodException: 
No signature of method: 
me.android.KF.activities.Login.KFLoginActivity.injectViews() is applicable for argument types: 
(me.android.KF.activities.Login.KFLoginActivity) values: 
[me.android.KF.activities.Login.KFLoginActivity@4274bee0]

Alright, I checked previous issues/answers, and it seems "SwissKnife.inject(this) " is not necessary? Then I comment it, and here comes another problem:

Problem 2: @OnClick(R.id.testBtn) never works.

P.S. I 'm a groovy player, and codes are in correct position under groovy/ folder as required. Any idea? Please help! Thanks

eugene-kamenev commented 9 years ago

Are you sure that your class is inside main/src/groovy and not in main/src/java folder?

newjing commented 9 years ago

yes, I'm doing everything according to requirements. I wrote everything in Groovy

jmartinesp commented 9 years ago

Let me guess: your view doesn't have any @InjectView annotations, does it?

newjing commented 9 years ago

No, I really don't have @InjectView in this activity.

I found where the problem is, so weird: If my activity is directly under "activities" package, everything works well. I created a sub package "Login" under "activities" and put it inside, it doesn't work. Why?

jmartinesp commented 9 years ago

I'll try to figure out what happens.

jmartinesp commented 9 years ago

Could you provide some sample? I'm not sure I can reproduce this bug.

newjing commented 9 years ago
  1. I create a subfolder as main/groovy/"my packageName"/activities/"subFolder name" , i.e. a subfolder of where normal activity classes stay (which is directly in "activities" folder as default)
  2. Then I create an activity "KFLoginActivity .groovy" in this subfolder, with the code in my first post above. In compiling, I got the error as described.

If I move this "KFLoginActivity .groovy" from the subfolder up to "acitivities" folder, everything works perfect. Today I repeated it in another project and reproduced this bug.

Thanks for you attention. SwissKnife is really a great tool!

P.S. I have another question, there's demo code in https://github.com/Arasthel/SwissKnife/blob/master/docs/dsl-methods.adoc : context.startActivity(AnotherActivity) { putExtra("key", value) } What is the correct "context" to use it? In an activity class, I tried "this" & getApplicationContext(), both caused error.

eugene-kamenev commented 9 years ago

@newjing Can you please fork SwissKnife project, then modify sample project to reproduce the bug. And give us a link. Thanks.

newjing commented 9 years ago

ok, I'll do it. That will take a while, coz I 'm behind the China "great wall", it takes some time to check out and check it.

jmartinesp commented 9 years ago

If you could do what @eugene-kamenev said, that would be great.

I've already tried it myself and I can't really make it fail...

Sample modified

As you can see, I created 2 levels of subpackaging and moved the BackgroundActivity class to the lower of them. Then I just hit run and the compilation works just fine. Maybe it's some configuration error or something :worried:.

jmartinesp commented 9 years ago

You can also upload a copy of the other project somewhere and we can take a look at it, there's no need to modify the sample that way.

EDIT: as for the startActivity issue, it's actually a bug, I forgot to set the closure parameter as optional, so unless you add a closure at the end it won't build.

newjing commented 9 years ago

I am so sorry @Arasthel & @eugene-kamenev . It must be sth wrong with my own projects , though I haven't found out where.

I tested with SwissKnife fork and it worked. Then I created a new project and it worked. My own project used lots of libraries which might cause conflict or sth. No idea though.

Thanks a lot for your efforts.