App is crashing when I run it [closed] - java

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);
}
}

Related

App keeps stopping with TextView.setText() method [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 2 years ago.
I am new to android programming and I can't figure out what i'm doing wrong.
My code:
Every time I run the code, the emulator works just fine, but when I try to open the app it show the error message.
Please help. Thanks.
first you must inflate your layout by setContentView method .
second in onCreate method , bind your view .
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity_layout);
TextView tf1 = (TextView) findViewById(R.id.tf0);
tf1.setText("Hello World!");
}
You must call setContentView before calling findViewById.
void onCreate() {
super.onCreate()
setContentView(R.layout.my_activity_layout)
TextView tf1 = findViewById(R.id.tf0)
tf1.setText("hello")
}
You need SetContentView(R.layout.your_layout) before calling findViewById.
For example:
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
tf1 = findViewById(R.id.tf0);
tf1.setText("text")
I suspect tf1 is null because the layout hasnt been inflated yet also setContentView(view) is required to be called in onCreate to inflate the view. Also at the point you are declaring the tf1 (outside onCreate) the reference will be null.
Try this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tf1 = (TextView) findViewById(R.id.tf0);
tf1.setText("Hello World!");
}

How to correct add a listener to a Fragment

I got two Fragments (basic-setup) with a FragmentPagerAdapter for use in a TabLayout.
tabLayout.setupWithViewPager(myViewPagerWithAdapter);
My Adapter takes an array of Fragments and titles as argument (also the FragmentManger for the super call).
MyViewPagerAdapter adapter = new MyViewPagerAdapter(getSupportFragmentManager() ,fragments, titles);
My Fragments (indeed 2) got a WebView in their xml file. To use the WebViews outside the fragments (it's kinda odd to code the control of the WebViews inside fragments, because the MainActivity is mainly involved with my user interaction). To know when my WebViews in the fragments were inflated, I added a simple interface, called OnFragmentCreatedListener, which is triggered in public void onActivityCreated(Bundle savedInstanceState) of the fragments. I add the listener through following way:
private OnFragmentCreatedListener listener;
...
public void addOnFragmentCreatedListener(OnFragmentCreatedListener listener) {
this.listener = listener;
}
In my MainActivity I add this listener like this:
public class MainActivity extends AppCompatActivity implements OnFragmentCreatedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
...
MyFragment fragment = MyFragment.newInstance();
...
fragment.addOnFragmentCreatedListener(this);
...
}
#Override
public void onFragmentCreatedListener() {
// Do my stuff
}
Now when the screen rotates and the activity is restarted, the listener in the fragment stays null (wasn't initialized). Now I don't know what to do, because I don't understand this behavior. The code should do the same like in the first protected void onCreate(Bundle savedInstanceState) of the MainActivity. It's adding the listener but it stays null (like wtf?).
Thank you for spending your time!

Extending library activity doesn't call onCreate

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 . :)

Error in AsyncTask onPostExecute

I'm trying to setText on a TextView, I get an error on the phone, and in the LogCat all I get is tag: androidruntime, text: at com.examp... onPostExecute (AsyncSomething.java).
Here is my pseudo code:
TextView display_car_tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
display_name_tv = (TextView) findViewById(R.id.display_name_tv);
.....
setContentView(R.layout...);
}
I have a button that affected to the methode that does only:
new AsyncSomething(this,display_car_tv).execute();
The code of AsyncSomething:
private Context context;
private TextView tv;
public AsyncSomethin(Context context, TextView tv){
this.context=context;
this.tv=tv;
}
protected String doInBackground(String...arg0){
//some code that works
return sb.toString();
}
protected void onPreExecute() {}
#Override
protected void onPostExecute(String result){
this.tv.append(result);
}
What do I do wrong?
How can I get more information from LogCat?
PS : sorry for not giving the entire code, but it is not relevant.
from the code you posted the only mistake I can see, probably leading your app to crash for NullPointerException, is that you are using findViewById before setContentView. You have always to call setContentView before every findViewByIds otherwise you will not have any widgets to find

Java syntax explanation

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.

Categories