Reputation: 848
I am developing a android app, i am facing force close issue when i am using "Toast.makeText" in my code, when i remove the line its working perfectly. Can anyone tell me the problem in this. i am pasting the screenshot of Log Cat.
Upvotes: 0
Views: 2039
Reputation: 3305
Ok. Class Activity is extended from class Context. To have reference to Context in Activity is not correct. When you want to show Toast, just put your activtiy as Context.
Class MainActivtiy extends Activty {
...
Toast.makeText(this,"Sending SMS..Please Wait..!!",Toast.LENGTH_SHORT).show();
...
}
If call in internal class, do just like that :
Class MainActivtiy extends Activty {
...
onClick (View v) {
Toast.makeText(MainActivty.this,"Sending SMS..Please Wait..!!",Toast.LENGTH_SHORT).show();
}
...
}
Good luck!
Upvotes: 2
Reputation: 4535
Accessing context from a Thread with this.context is giving you NULLPointerException, as Thread class structure belong to classic Java and does not posses the Android Context. For example, you tried to access like 'this.context' which is null. As @SamirMangroliya suggested use current activity or use Android way of multithreading ASynchTask which posses context reference
Note this was a comment, but could't find the original response so here it is
Upvotes: 0
Reputation: 3846
try
Toast.makeText(this, "Sending SMS..Please Wait..!!",Toast.LENGTH_SHORT).show();
Upvotes: 0
Reputation: 848
Found the Solution..It goes like this
Toast.makeText(MainActivity.this, "Sending SMS..Please wait..!!",Toast.LENGTH_SHORT).show();
Upvotes: 0
Reputation: 16354
Toast.makeText(MainActivity.this, "Sending SMS..Please Wait..!!",Toast.LENGTH_SHORT).show();
Upvotes: 3
Reputation: 2824
You can use getApplicationContext() or Your_Class_Name.this rather than using this.context then i think this problem will be solved.
Upvotes: 0
Reputation: 1066
context
variable has no value.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
}
Upvotes: 0