Textview value is crashing with null pointer exception
Below is the code which i am using. Is there any issue in the code.
public class MainActivity extends Activity {
Button btn;
int i =0;
SharedPreferences.Editor preferences;
TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.button1);
preferences = PreferenceManager.getDefaultSharedPreferences(MainActivity.this).edit();
txt = (TextView) findViewById(R.id.textView1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
new LongOperation().execute("");
}
});
}
private class LongOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
for (int i = 0; i < 5; i++) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
Thread.interrupted();
}
}
return "Executed";
}
#Override
protected void onPostExecute(String result) {
txt.setText("Executed ----------------------- ");
}
#Override
protected void onPreExecute() {}
#Override
protected void onProgressUpdate(Void... values) {}
}
}
After it is coming to onPostExecute the textview value is not setting the value.
Please help me. Thanks in advance.
Textview value is not setting because the value of id may be null. check it.
Related
i am trying to insert checkbox value into my mysql database, there is no error in the program...
But i cannot get the value into the database.
In the database it show empty...
public class MainActivity extends AppCompatActivity {
CheckBox box;
Button btn_test;
private static final String REGISTER_URL="http://192.168.0.106/test/checkin.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
box = (CheckBox) findViewById(R.id.checkBox);
btn_test = (Button) findViewById(R.id.button);
btn_test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
registerUser();
}
});
}
private void registerUser() {
box.isChecked();
String date = box1.toString().trim().toLowerCase();
register(date);
}
private void register(String date){
String urlSuffix = "?date=" + date;
class RegisterUser extends AsyncTask<String, Void, String> {
how can i get the value from the check box and inserted in the database
Please try this,May be it will help you :)
box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked){
String date = box1.toString().trim().toLowerCase();
register(date); } } } );
I am facing strange issue during call of my activity with Fragment usage. I am getting error like,
java.lang.IllegalStateException: Fragment ScoreFragment{ee2b833
id=0x7f0e0198} not attached to Activity
On line 146. My Fragment code which have error is line like below
if(mPageFlag.equalsIgnoreCase(getString(R.string.winners))){
And My full code for same is below,
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mPageFlag = getArguments().getString(ARG_PAGE_FLAG);
}
}
#Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser && !mIsPageLoaded){
mContext = getActivity();
mIsPageLoaded = true;
if(mPageFlag.equalsIgnoreCase(getString(R.string.winners))){
new getcontestscorewinners(mContext).execute();
}else{ //
new getcontestscorewinnersNew(mContext).execute();
}
}
}
public class getcontestscorewinners extends AsyncTask<String, Void, String> {
boolean response = false;
private Context mContext;
public getcontestscorewinners(Context context) {
mContext = context;
}
#Override
protected void onPreExecute() {
progress = ProgressDialog.show(mContext, "Processing...",
"Please wait....");
}
#Override
protected String doInBackground(String... params) {
NetworkTask.getContestScoreWinners(winnerHandler);
return "";
}
#Override
protected void onPostExecute(String result) {
}
#Override
protected void onProgressUpdate(Void... values) {
}
}
Let me know if someone can help me for get out of it. Thanks.
Try using isAdded():
Return true if the fragment is currently added to its activity.
So your code should be like this :
if(isAdded() && mPageFlag.equalsIgnoreCase(getString(R.string.winners))){
I am trying the apply this example to my project but I am getting the following error:
Error:(26, 33) error: cannot find symbol method findViewById(int)
This is how my code looks like:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView display = (TextView) findViewById(R.id.textView1);
display.setText("Hello Android!");
Connect myConnect = new Connect();
myConnect.execute();
}
}
class Connect extends AsyncTask<Void, Void, Integer> {
TextView Text1 = (TextView) findViewById(R.id.textView1);
#Override
protected void onPreExecute(){
}
#Override
protected Integer doInBackground(Void... params) {
return 0;
}
#Override
protected void onProgressUpdate(Void... values) {
super.onPostExecute(0);
Text1.setText("Text Changed");
}
}
What am I missing here?
How can I reference the TextView through the AsyncTask?
This is incorrect :
class Connect extends AsyncTask<Void, Void, Integer> {
TextView Text1 = (TextView) findViewById(R.id.textView1);
findViewById is a method of Activity class. not AsyncTask
to fix it, use this:
Connect myConnect = new Connect(display);
and create constructor:
class Connect extends AsyncTask<Void, Void, Integer> {
TextView Text1;
public Connect(TextView tv) {
Text1 = tv;
}
and you have got the other error:
#Override
protected void onProgressUpdate(Void... values) {
super.onPostExecute(0);
Text1.setText("Text Changed");
}
change it into this:
#Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
Text1.setText("Text Changed");
}
SUMMARISE
//one file
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView display = (TextView) findViewById(R.id.textView1);
display.setText("Hello Android!");
Connect myConnect = new Connect(display);
myConnect.execute();
}
}
//possible the other file. but you can put the class below as nested - just move previous paranthesis to the end of the code
class Connect extends AsyncTask {
TextView Text1;
public Connect(TextView tv) {
Text1 = tv;
}
#Override
protected void onPreExecute(){
}
#Override
protected Integer doInBackground(Void... params) {
/*publish your progress here
publishProgress(...);
*/
return 0;
}
#Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
Text1.setText("Text Changed");
}
}
Your text view is related to your MainActivity so create the global reference in activity class only and use it for sub class also.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.AsyncTask;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView Text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Text1 = (TextView) findViewById(R.id.textView1);
Text1.setText("Hello Android!");
Connect myConnect = new Connect();
myConnect.execute();
}
class Connect extends AsyncTask<Void, Void, Integer> {
#Override
protected void onPreExecute(){
}
#Override
protected Integer doInBackground(Void... params) {
return 0;
}
#Override
protected void onProgressUpdate(Void... values) {
super.onPostExecute(0);
Text1.setText("Text Changed");
}
}
}
Use your view inside onCreate(). onProgressUpdate() will be called on the main application thread (a.k.a., UI thread). As said by CommansWare
Global
private TextView Text1;
use as:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView display = (TextView) findViewById(R.id.textView1);
Text1 = (TextView) findViewById(R.id.textView1); //here
.........
Set as it is
#Override
protected void onProgressUpdate(Void... values) {
super.onPostExecute(0);
Text1.setText("Text Changed");
}
findViewById() is not accessible inside AsyncTask, the method belongs to activity, so you cannot initialize view using id inside Asynctask. Initialize your view in activity inside onCreate and use the instance in async task
public class MainActivity extends AppCompatActivity {
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (TextView) findViewById(R.id.textView1);
display.setText("Hello Android!");
Connect myConnect = new Connect();
myConnect.execute();
}
class Connect extends AsyncTask<Void, Void, Integer> {
#Override
protected void onPreExecute(){
}
#Override
protected Integer doInBackground(Void... params) {
return 0;
}
#Override
protected void onProgressUpdate(Void... values) {
}
protected void onPostExecute(Long result) {
display.setText("Text Changed");
}
}}
Hi i am new to programming. I want access arraylist outside from the onCreate but i get error.Below is code.
public class WelcomeOnFootActivity extends AppCompatActivity {
ImageButton next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_on_foot);
Bundle bundle=getIntent().getExtra;
List<UserDate> data= new ArrayList();
data.add((UserData)bundle.get("data"));
next=(ImageButton)findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handle();
}
});
}
private void handle(){
String info=data.getUserInfo;// here i get error
}
}
Just declare the list globally at class level. In your case before OnCreate.
List<UserDate> data= new ArrayList();
en#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_on_foot);
Bundle bundle=getIntent().getExtra;
data.add((UserData)bundle.get("data"));
next=(ImageButton)findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handle();
}
});
}
private void handle(){
String info=data.getUserInfo;
}
Hope it will help.
public class WelcomeOnFootActivity extends AppCompatActivity {
ImageButton next;
List<UserDate> data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_on_foot);
Bundle bundle=getIntent().getExtra;
data= new ArrayList();
data.add((UserData)bundle.get("data"));
next=(ImageButton)findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handle();
}
});
}
private void handle(){
String info=data.getUserInfo;// here i get error
}
}
public class WelcomeOnFootActivity extends AppCompatActivity {
ImageButton next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_on_foot);
Bundle bundle=getIntent().getExtra;
final List<UserDate> data= new ArrayList();
data.add((UserData)bundle.get("data"));
next=(ImageButton)findViewById(R.id.next);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
handle(data);
}
});
}
private void handle(List<UserDate> listUD){
String info=listUD.getUserInfo;// here i get error
}
}
I'm attempting to extract data from a table on a web page. Thus far I've been able to extract the data from the title tag - but not the table data. How can this be accomplished using the source shown below?
SOURCE:
public class MainActivity extends Activity {
TextView tv;
final String URL="http://example.com";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.TextView01);
new MyTask().execute(URL);
}
private class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog prog;
String title = "";
#Override
protected void onPreExecute() {
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Loading....");
prog.show();
}
#Override
protected String doInBackground(String... params) {
try {
Document doc = Jsoup.connect(params[0]).get();
title = doc.title();
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
tv.setText(result);
}
}
}
EDIT - In Response to Matej Spili's comment below:
public class MainActivity extends Activity {
TextView tv;
final String URL="http://example.com";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.TextView01);
new MyTask().execute(URL);
}
private class MyTask extends AsyncTask<String, Void, String> {
ProgressDialog prog;
String title = "";
#Override
protected void onPreExecute() {
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Loading....");
prog.show();
}
#Override
protected String doInBackground(String... params) {
try {
Document doc = Jsoup.connect(params[0]).get();
Element tableHeader = doc.select("tr").first();
for( Element element : tableHeader.children() )
{
// Here you can do something with each element
System.out.println(element.text());
tv.setText(element.text());
}
title = doc.title();
} catch (IOException e) {
e.printStackTrace();
}
return title;
}
#Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
prog.dismiss();
tv.setText(result);
}
}
}
^- (causes error: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views)
Pass the view that you want to update as constructor argument for the MyTask like
new MyTask(tv).execute(URL);
Change your MyTask accordingly to accept this argument.