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.
Related
I'm very new to Android and Java coding in general, so bear with me if I don't understand basic concepts.. This is just a test to see if something works, so It might not initially make any sense.
I have two activities, Main and Other.
the Other Activity has an imageButton with visibility initially set to invisible.
When MainActivity is created, it should look for that imageButton in the Other activity, and set it's view to visible.
Though when debugging, all I get is a nullPointerException, because the button has null as value. How can I make it reference the button?
Part of Main Activity:
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pstep();
}
public void pstep() {
int pstep = 0;
ImageButton panfav = (ImageButton) findViewById(R.id.favpancake);
panfav.setVisibility(View.VISIBLE);
}
Part of Other Activity:
public class navfav extends AppCompatActivity {
ImageButton panfav = (ImageButton) findViewById(R.id.favpancake);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navfav);
}
public void fab(View v){
ImageButton panfav = (ImageButton) findViewById(R.id.favpancake);
panfav.setVisibility(View.VISIBLE);
}
XML linked to Other Activity:
<ImageButton
android:id="#+id/favpancake"
android:layout_width="wrap_content"
android:layout_height="137dp"
android:layout_alignParentStart="true"
android:layout_below="#+id/imageView3"
android:background="#android:color/background_dark"
android:contentDescription="#string/nav_cook_dish"
android:onClick="fab"
android:scaleType="centerCrop"
android:src="#drawable/dishpancake"
android:visibility="invisible" />
Log:
java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.widget.ImageButton.setVisibility(int)' on a null object reference
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method
'void android.widget.ImageButton.setVisibility(int)' on a null object reference
at com.p2.rookie.MainActivity.pstep(MainActivity.java:23)
at com.p2.rookie.MainActivity.onCreate(MainActivity.java:16)
Shoot if I'm missing anything you need to use, I'll provide as much as i can :)
Best
The visibility of the buttons are always "VISIBLE", please give me the log
and in the navFav activity the "fab" method are not in the onCreate
you are getting nullPointerException because ImageButton with id favpancake is not present in MainActivity layout activity_main
public class navfav extends AppCompatActivity {
public static List<String> recipes;
// other code here
}
and then when you want to add recipe to shown in Other Activity listView,
recipes = new ArrayList<String>();
recipes.add(recipe);
Now when you open the other Activity all those recipes will be available in list name "recipes". which you can use to show in your listView.
One Activity can and should not reference elements in other Activities.
If you call 'setContentView' with a layout file, you can only reference the views in that layout file.
Try and tell us what you're trying to achieve and we might be able to help you out in improving your code.
(Posted on behalf of the OP).
Just read a bunch of other stuff elsewhere, and I think something made sense now! Instead of creating new intents and startActivity'ies when changing layout/pages, I just setContentView to new layouts/pages, and gather all methods from different activities in one activity - That's how basic this fail was I think? ;)
Then all views and methods can reference each other.
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 . :)
I'm launching a new activity (TVMPDFActivity) from my root activity. I'm using Android Annotations, so this is how I'm launching the Activity:
TVMPDFActivity_.intent(this.getActivity()).start();
TVMPDFActivity is a subclass of PDFPreviewActivity which is a subclass of ActionBarActivity.
A NullPointerException is being thrown from onCreate in PDFPreviewActivity when I simply try to GET the ActionBar. On this line:
final ActionBar actionBar = getSupportActionBar();
Here is the stacktrace from my code to the NPE:
Caused by: java.lang.NullPointerException
at android.support.v7.internal.app.WindowDecorActionBar.getDecorToolbar(WindowDecorActionBar.java:248)
at android.support.v7.internal.app.WindowDecorActionBar.init(WindowDecorActionBar.java:201)
at android.support.v7.internal.app.WindowDecorActionBar.<init>(WindowDecorActionBar.java:176)
at android.support.v7.app.ActionBarActivityDelegateBase.createSupportActionBar(ActionBarActivityDelegateBase.java:156)
at android.support.v7.app.ActionBarActivityDelegate.getSupportActionBar(ActionBarActivityDelegate.java:123)
at android.support.v7.app.ActionBarActivity.getSupportActionBar(ActionBarActivity.java:73)
at com.my.app.PDFPreviewActivity.onCreate(PDFPreviewActivity.java:63)
at com.my.app.TVMPDFActivity.onCreate(TVMPDFActivity.java:24)
at com.my.app.TVMPDFActivity_.onCreate(TVMPDFActivity_.java:31)
Here are the onCreate methods of the various classes involved:
public class TVMPDFActivity extends PDFPreviewActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
this.setContentView(R.layout.tvm_pdf_activity);
super.onCreate(savedInstanceState);
...
}
}
public abstract class PDFPreviewActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActionBar actionBar = getSupportActionBar(); // throws NullPointerException
...
}
}
Why am I getting a NullPointerException from deep within the support v7 code?
you should call super.onCreate before setContentView in TVMPDFActivity in order to give android the opportunity to initialize its internal state, before trying to access something that relies on that internal state. For onCreate/onStart/onResume call the super always as first statement
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