I have added a project 'A' as library in my project 'B'. Now in project 'B', I have an activity 'MainActivity' which extends an activity 'SplashActivity' of project A.
When i'm running the app, the onCreate of 'MainActivity'(Project B) is never called, rather the OnCreate of 'SplashActivity'(Project A) is called every time. What could be the problem?
Also what if I want to call the onCreate method of 'SplashActivity'(Project A) and then the onCreate of 'MainActivity'(Project B), is it possible?
The code is given below:
public class MainActivity extends SplashActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_activity_temp);
new SplashActivity().setLogo();//never called
}
}
change your xml ref
setContentView(R.layout.activity_splash_activity_temp);
new SplashActivity().setLogo();
to
setContentView(R.layout.activity_main);
SplashActivity splashactivity = new SplashActivity();
splashactivity.setLogo();
I solved my own answer ,and thanks to #jimit patel and # ρяσѕρєя K , I solved it and and if anyone looking for an answer
Code :
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_splash_activity_temp);
ImageView imageView = (ImageView) findViewById(R.id.splash_logo);
super.setLogo(imageView,this);
super.onCreate(savedInstanceState);
}
Just call the super.onCreate at the end of the code , and the code will work just as expected . :)
Related
Is there a way I can use two classes or more for one Activity in android studio?
I used this Test code but this App crashes:
Note : This is for learning purposes so that it can be used to split up huge classes into sub classes
//Main Activity Class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Test ob=new Test ();
ob.test();
}
}
// Test Class
public class Test extends MainActivity {
public void test()
{
TextView t=findViewById(R.id.h);
t.setText("Miaooo");
}
}
What you can do is to pass the activity with as the parameter of constructor and use that reference to call "findViewbyId()"
public class Test{
public void test(Activity activity)
{
TextView t=activity.findViewById(R.id.h);
t.setText("Miaooo");
}
while in your main activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Test ob=new Test (this);
ob.test();
}
If you want to your "Test" class to be extended by an activity than you should use activity creation wizard to setup all the stuff in manifest and .xml. It would be quite long process to do it manually.
When you write class Test extends MainActivity it means that Test should be activity too and you have to write setContentView(R.layout.your_layout); to set the view layer for your activity. Also, Test should be registered as Activity in your manifest file. Anyway if you want to change the text of the TextView why you want to create a new activity?
place your code in the activity main.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test();
}
public void test()
{
TextView t=findViewById(R.id.h);
t.setText("Miaooo");
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I do not get any errors but when I run the app it crashes:
public class MainActivity extends AppCompatActivity {
ImageButton Nr1 = (ImageButton) findViewById(R.id.ImageButton1);
public void machwas(View v) {
Nr1.setImageResource(R.drawable.x);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
This here is wrong:
public class MainActivity extends AppCompatActivity {
ImageButton Nr1 = (ImageButton)findViewById(R.id.ImageButton1);
because findViewById() should be called INSIDE the onCreate method, the reason is that after that are layouts inflated and you are ready to get the views and initialize the widgets...
so what you are doing is initializing the ImageButton wrongly, probably will be null after that line and then this here
public void machwas(View v){
Nr1.setImageResource(R.drawable.x);
}
is going to explode with a NullPointer Exception...
but this is only an inference because you dont post the LogCat...
For more info please take a look to the Activity lifeCycle and the Official Android Documentation
findViewById() searches the view that is set by setContentView(). In your code findViewById() is called before onCreate() so there's no view set yet and due to that all finds will fail and you will always get null there.
BTW: consider using helpers like ButterKnife
You have to call findViewById after view created.
public class MainActivity extends AppCompatActivity {
ImageButton Nr1 ;
public void machwas(View v){
Nr1.setImageResource(R.drawable.x);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Nr1 = (ImageButton)findViewById(R.id.ImageButton1);
machwas(0);
}
}
I got this weird problem and it's bugging me. I don't understand why I get this NullPointerException when I did initialise chronometer in MainActivity. If I initialise chronometer in SecondActivity the program works fine.
Hopefully someone can clear this up...
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Chronometer.setOnChronometerTickListener(android.widget.Chronometer$OnChronometerTickListener)' on a null object reference
-
public class MainActivity extends AppCompatActivity {
protected Chronometer chronometer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//chronometer gets initialised here but it's still null appearantly
chronometer = (Chronometer) findViewById(R.id.chronometer1);
//this works
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#cd2626")));
}
}
-
public class SecondActivity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
//if chronometer gets initialised again, program does work.
//chronometer = (Chronometer) findViewById(R.id.chronometer1)
}
}
include setContentView(R.layout.main_layout); in your mainactivity otherwise findviewbyid will not be able to find chronometer and it will always show null exception
Your chronometer variable is not initialized in MainActivity.onCreate because layout is inflated (setContentView called) only in SecondActivity.onCreate. So findViewById in your MainActivity has no effect.
In general I recommend not to build class hierarchies aroun Activity. Try to use something else, like interfaces to achieve what you need.
I'm new to Java and I encountered the following code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = new Button(this);
button.setText("Touch That!");
button.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.this.onButtonClick(v);
}
});
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rootlayout);
relativeLayout.addView(button);
}
public void onButtonClick(View view){
//do something when button is clicked.
}
}
I didn't understand the syntax, View.OnClickListener() c'tor is called and it is followed by {} and overidding method.
what does this syntax stand for?
to which object this refers?
My guess is the button. but if I'm right why to use MainActivity.this instead of this? (the object that invoked the method)
This is an anonymous class declaration. It means that you will override some methods inside the class dynamically.
Take a look at this arcticle:
http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
About your second question, MainActivity.this refers to the instance of the Activity you are currently in. If you call only this, it would refer to the actual object. When you call MainActivity.this, you will get the instance of MainActivity you are in, even if there is more activities created. Take a look at Android's activity lifecycle.
What's the difference between this and Activity.this
Hope it helps.
By calling
new View.OnClickListener(){}
you are creating an object implementing interface OnClickListerner that requires you to implement the click method.
Someone can correct if I am wrong.
I've got a problem with fragmentactivity and onCreate method. My classes looks like this:
public abstract class B extends FragmentActivity implements View.OnKeyListener
{}
public class A extends B
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// stuff
}
}
OnCreate in class A is not called. I created layout from another after click and onCreate is in second layout. I don't know where is the problem.
EDIT:
leandrocastelli - i started new layout simply by creating object new A(getActivity()) not by intend.
Arash - you have right. I've to start new Activity. And now startActivity throws nullpointer :/
blackbelt - it was create like above and now i've switch to startActivity.
You extend A class from FragmentActivity so your A class which is a FragmentActivity first should be called someway by startActivity,if you didn't do that so your subclass's onCreate would never get called