At the moment when running I get only one text displayed Wrong_answer.
public class AnswerActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_answer);
TextView textViewDisplayResult = (TextView) findViewById(R.id.text_view_display_result);
textViewDisplayResult.setText(getIntent().getExtras().getString("KEY_ALL_CHECKED"));
textViewDisplayResult.setText(getIntent().getBooleanExtra("KEY_ANSWER", false)?R.string.Good_answer:R.string.Wrong_answer);
}
POST UPDATE
public class MainActivity extends AppCompatActivity {
private static int NUMBER_OF_QUESTIONS = 3;
static boolean[] answer = new boolean[NUMBER_OF_QUESTIONS];
static boolean[] checked = new boolean[NUMBER_OF_QUESTIONS];
static boolean[] isAnswered = new boolean[NUMBER_OF_QUESTIONS];
final Intent intent = new Intent(MainActivity.this, AnswerActivity.class);
buttonCheckAnswer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!allAnswersChecked())
intent.putExtra("KEY_ALL_CHECKED", R.string.text_not_checked);
else if (checkAnswers())
intent.putExtra("KEY_ANSWER", R.string.Good_answer);
else
intent.putExtra("KEY_ANSWER", R.string.Wrong_answer);
startActivity(intent);
}
});
public static void checkSelected() {
for (boolean radioChecked : checked) {
if (radioChecked) {
buttonCheckAnswer.setVisibility(View.VISIBLE);
break;
}
}
}
private boolean checkAnswers() {
for (boolean radioAnswer : answer) {
if (!radioAnswer) {
return false;
}
}
return true;
}
private boolean allAnswersChecked() {
for (boolean radioAnswer : isAnswered) {
if (!radioAnswer) {
return false;
}
}
return true;
}
and AnswerActivity
public class AnswerActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_answer);
TextView textViewDisplayResult = (TextView) findViewById(R.id.text_view_display_result);
textViewDisplayResult.setText(getIntent().getExtras().getString("KEY_ALL_CHECKED"));
textViewDisplayResult.setText(getIntent().getBooleanExtra("KEY_ANSWER", false)?getString(R.string.Good_answer):getString(R.string.Wrong_answer));
}
}
Second setText will overwrite your first setText value.
....
String x = getIntent().getExtras().getString("KEY_ALL_CHECKED");
textViewDisplayResult.setText(getIntent().getBooleanExtra("KEY_ANSWER", false) ? x +" "+ R.string.Good_answer: x +" "+ R.string.Wrong_answer);
textViewDisplayResult will always overwrite.
If you want both to display in same textView you just need to append the text like below:
textViewDisplayResult.setText(getIntent().getExtras().getString("KEY_ALL_CHECKED"));
textViewDisplayResult.append(getIntent().getBooleanExtra("KEY_ANSWER", false)?getString(R.string.Good_answer):getString(R.string.Wrong_answer));
This will display your both result in same textView.
Related
I want all ClickableSpans to have toasts show their own text. But whatever I write in Toast in onClick in for loop, all ClickableSpans show same Toast output. Briefly, I just want ClickableSpans to show different Toasts in for loop. How can I do it? I want all words have clickable. And when all words are clicked, they do different thing. Thank you for your help.
public class MainActivity extends AppCompatActivity {
private String[] textArray;
private String text = "1981 senesinde kuantum bilgisayar fikrini Paul Beniof, Max Planck’ın " +
"enerjinin sürekli olmadan kesikli değerlerde yer alan m, n, k enerji kuantlarıyla ";
private Chronometer chronometer;
private int hold=0;
private TextView textView;
private int i;
private MediaPlayer player;
private long pauseOffset;
private boolean running;
private boolean start=true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textArray = text.split(" ");
final SpannableString ss = new SpannableString(text);
chronometer = findViewById(R.id.chronometer);
chronometer.setFormat("Time: %s");
chronometer.setBase(SystemClock.elapsedRealtime());
textView = findViewById(R.id.text_view);
textView.setText(text);
textView.setTextColor(Color.BLACK);
final ClickableSpan[] clickableSpans = new ClickableSpan[textArray.length];
for(i = 0; i<textArray.length; i++){
clickableSpans[i] = new ClickableSpan() {
#Override
public void onClick(View widget) {
Toast.makeText(MainActivity.this, "", Toast.LENGTH_LONG).show();
}
#Override
public void updateDrawState(TextPaint ds) {
super.updateDrawState(ds);
ds.setColor(Color.BLACK);
ds.setUnderlineText(false);
}
};
final boolean[] c = {true};
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
#Override
public void onChronometerTick(Chronometer chronometer) {
if ((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 5000&& c[0]) {
for(int i=0; i<textArray.length;i++){
ss.setSpan(clickableSpans[i], hold, hold + textArray[i].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
hold+=textArray[i].length();
hold++;
}
start=false;
hold=0;
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
if (running) {
chronometer.stop();
pauseOffset = SystemClock.elapsedRealtime() - chronometer.getBase();
running = false;
}
c[0]=false;
}
}
});
hold=0;
}
i=0;
}
public void play(View v) {
if(start){
if (!running) {
chronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
chronometer.start();
running = true;
}
}
}
public void stop(View v) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
I'm working on a project for Shoe sticker using estimote sticker and facing some problems. I'm using android studio for my project
I have error at dbRow = stickerdb.getResult(currentNearable.identifier("065473d63cd9d687"));
It says Method call expected
The error log:
Error:(96, 52) error: cannot find symbol method identifier(String)
public class NearablesDemoActivity extends BaseActivity {
private static final String TAG = NearablesDemoActivity.class.getSimpleName();
private Nearable currentNearable;
private BeaconManager beaconManager;
private String scanId;
TextView Desc; //description
Spinner spinnerDropDown; //for size available
String[] size = {
}; //for size available
Spinner spinnerDropDown2; //for colours available
String[] colour = {
}; //for colours available
TextView COO; //For country of origin
TextView SM; //For Shoe Model
TextView Price; //For price
Button btnRating; //for button
private Database_sticker stickerdb;
Sresult dbRow;
#Override
protected int getLayoutResId() {
return R.layout.nearable_demo;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nearable_demo);
currentNearable = getIntent().getExtras().getParcelable(ListNearablesActivity.EXTRAS_NEARABLE);
displayCurrentNearableInfo();
beaconManager = new BeaconManager(this);
ArrayAdapter<String> adapter= new ArrayAdapter<>(this,android.R.layout.simple_spinner_dropdown_item ,size); //for size available
spinnerDropDown.setAdapter(adapter);
ArrayAdapter<String> adapter1= new ArrayAdapter<>(this,android.R.layout.simple_spinner_dropdown_item ,colour); //for colours available
spinnerDropDown2.setAdapter(adapter1);
toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_action_navigation_arrow_back);
toolbar.setTitle(getTitle());
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
btnRating=(Button)findViewById(R.id.rd);
btnRating.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent pageforRatingBar = new Intent(getApplicationContext(), RatingBar_main.class);
startActivity(pageforRatingBar);
}
});
stickerdb = new Database_sticker(this);
// dbRow = stickerdb.getResult(1);//currentNearable.identifier()
dbRow = stickerdb.getResult(currentNearable.identifier("065473d63cd9d687"));
dbRow.getSa();
dbRow.getDesc();
dbRow.getCoo();
dbRow.getId();
dbRow.getPrice();
dbRow.getSm();
}
#Override
protected void onResume() {
super.onResume();
beaconManager.setNearableListener(new BeaconManager.NearableListener() {
#Override
public void onNearablesDiscovered(List<Nearable> nearables) {
updateCurrentNearable(nearables);
displayCurrentNearableInfo();
}
});
beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
#Override
public void onServiceReady() {
scanId = beaconManager.startNearableDiscovery();
}
});
}
#Override
protected void onStop() {
beaconManager.disconnect();
super.onStop();
}
private void displayCurrentNearableInfo() {
Desc = (TextView) findViewById(R.id.textview1); //for description
// Get reference of SpinnerView from layout/nearable_demo.xml
spinnerDropDown = (Spinner) findViewById(R.id.spinner1); //For size available
spinnerDropDown2 = (Spinner) findViewById(R.id.spinner2);//for colours available
COO = (TextView) findViewById(R.id.textview2);//for country of origin
SM = (TextView) findViewById(R.id.textview3); //for shoe model
Price = (TextView) findViewById(R.id.textview4); //for price
}
private void updateCurrentNearable(List<Nearable> nearables) {
for (Nearable nearable : nearables) {
if (nearable.equals(currentNearable)) {
currentNearable = nearable;
}
}
}
}
You're calling an identifier method on an object of the Nearable class. There is no such method in this class:
http://estimote.github.io/Android-SDK/JavaDocs/com/estimote/sdk/Nearable.html
There is, however, an identifier property, and that's the one you need to use:
http://estimote.github.io/Android-SDK/JavaDocs/com/estimote/sdk/Nearable.html#identifier
In other words, this line:
dbRow = stickerdb.getResult(currentNearable.identifier("065473d63cd9d687"));
… needs to become:
dbRow = stickerdb.getResult(currentNearable.identifier);
I have a class like this:
public class TimerActivity extends Activity
{
CountDownTimer cntTimer = null;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timers);
final ImageButton startTimerButton = (ImageButton)findViewById(R.id.timer1ImageButton);
final EditText timerText = (EditText) findViewById(R.id.timerEditText1);
final TextView timerTextValue = (TextView) findViewById(R.id.timerTextView);
boolean checkstate =false;
timerCountDown(checkstate,startTimer3Button,timerText3, timerTextValue3);
}
public void timerCountDown(boolean check,final ImageButton startTimerImageButton ,
final EditText timerText,final TextView timerTextValue)
{
Integer input = 0;
if(timerText.getText().toString()!="")
{
input = Integer.parseInt(timerText.getText().toString())*1000 ;
}
CountDownTimer timer = new CountDownTimer(input, 1000)
{
public void onTick(long millisUntilFinished)
{
timerTextValue.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish()
{
timerTextValue.setText("done!");
}
};
timerStatus(check,startTimerImageButton,timer);
}
public void timerStatus(final boolean checkstate, final ImageButton startTimer3Button ,final CountDownTimer downTimer)
{
startTimer3Button.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
if(checkstate==false)
{
startTimer3Button.setImageResource(R.drawable.reset);
//Error
checkstate = true;
downTimer.start();
}
else
{
startTimer3Button.setImageResource(R.drawable.start);
//Error
checkstate = false;
downTimer.cancel();
}
}
});
}
}
but I get this error on the timerStatue method for checkstate = false and checkstate = true:
The final local variable checkstate cannot be assigned, since it is defined in an enclosing type!
I searched google and stackoverflow but did not find compatible answer for my problem!
Can you help me?
thanks in advance!
I don't know want you meant to do with your code. It's a little fuzzy. As the Yazan said it's better to implement OnClickListener interface to avoid sending final to it's method.
After that why everything is final? You must define your variables in class level (class variables) to access them from other methods easily and the onCreate() method set their primary values.
So whatever I fixed your code like below without any error but I still didn't what It meant to do :
public class TimerActivity extends Activity
{
CountDownTimer cntTimer = null;
ImageButton startTimerButton;
EditText timerText;
TextView timerTextValue;
boolean checkstate =false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timers);
startTimerButton = (ImageButton)findViewById(R.id.timer1ImageButton);
timerText = (EditText) findViewById(R.id.timerEditText1);
timerTextValue = (TextView) findViewById(R.id.timerTextView);
timerCountDown();
}
public void timerCountDown()
{
Integer input = 0;
if(timerText.getText().toString()!="")
{
input = Integer.parseInt(timerText.getText().toString())*1000 ;
}
CountDownTimer timer = new CountDownTimer(input, 1000)
{
public void onTick(long millisUntilFinished)
{
timerTextValue.setText("seconds remaining: " + millisUntilFinished / 1000);
}
public void onFinish()
{
timerTextValue.setText("done!");
}
};
timerStatus(timer);
}
public void timerStatus(final CountDownTimer downTimer)
{
startTimerButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
if(checkstate==false)
{
startTimerButton.setImageResource(android.R.drawable.ic_secure);
//Error
checkstate = true;
downTimer.start();
}
else
{
startTimerButton.setImageResource(android.R.drawable.ic_delete);
//Error
checkstate = false;
downTimer.cancel();
}
}
});
}
}
you can make a class that implements OnClickListener or even your current class can do that, and override onclick() in that way you can access any variable (not only final)
example: Edit
1) make you activity implements OnClickListener
2) override the method onClick() in your activity.
3) move boolean checkstate =false; at class level (outside of onCreate())
4) change startTimer3Button.setOnClickListener(... to startTimer3Button.setOnClickListener(TimerActivity.this);
6) move the code inside onClick() that exist in timerStatus() to the onclick() added to the activity class
public class TimerActivity extends Activity implements OnClickListener{
//code and methods ....
public void timerStatus(final boolean checkstate, final ImageButton startTimer3Button ,final CountDownTimer downTimer)
{
startTimer3Button.setOnClickListener(TimerActivity.this);
}
//code and methods ....
#override
public void onClick(View v){
if(checkstate==false)
{
startTimer3Button.setImageResource(R.drawable.reset);
//Error
checkstate = true;
downTimer.start();
}
else
{
startTimer3Button.setImageResource(R.drawable.start);
//Error
checkstate = false;
downTimer.cancel();
}
}
}
So I am in the middle of a Challenge from a book I am reading. The challenge says that I do some questions at the users of the app and they should answer True or False. Also they have the option to cheat but when they gone back to the question answer must not be acceptable.
So I created a Boolean Array where I save the status of the "cheater". True if he cheats and False if he doesn't cheat. But after one full rotation of the Questions the Array is Wiped. Why is this happening? What I am doing wrong?
Here is my main code for the Quiz Activity:
public class QuizActivity extends Activity {
private Button mTrueButton;
private Button mFalseButton;
private Button mNextButton;
private Button mCheatButton;
private TextView mQuestionTextView;
private static final String TAG = "QuizActivity";
private static final String KEY_INDEX = "index";
private static final String CHEATER_BOOLEAN = "cheat";
private TrueFalse[] mQuestionBank = new TrueFalse[] {
new TrueFalse(R.string.question_oceans, true),
new TrueFalse(R.string.question_mideast, false),
new TrueFalse(R.string.question_africa, false),
new TrueFalse(R.string.question_americas, true),
new TrueFalse(R.string.question_asia, true),
};
private boolean[] mCheatBank = new boolean[mQuestionBank.length];
private int mCurrentIndex = 0;
private boolean mIsCheater;
private void updateQuestion() {
int question = mQuestionBank[mCurrentIndex].getQuestion();
mQuestionTextView.setText(question);
}
private void checkAnswer(boolean userPressedTrue) {
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
int messageResId = 0;
if(mIsCheater) {
messageResId = R.string.judgment_toast;
} else {
if (userPressedTrue == answerIsTrue) {
messageResId = R.string.correct_toast;
} else {
messageResId = R.string.incorrect_toast;
}
}
Toast.makeText(this, messageResId, Toast.LENGTH_SHORT)
.show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate(Bundle) called");
setContentView(R.layout.activity_quiz);
mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
mQuestionTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
mIsCheater = false;
updateQuestion();
}
});
mTrueButton = (Button)findViewById(R.id.true_button);
mTrueButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkAnswer(true);
}
});
mFalseButton = (Button) findViewById(R.id.false_button);
mFalseButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
checkAnswer(false);
}
});
mNextButton = (Button)findViewById(R.id.next_button);
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
mIsCheater = mCheatBank[mCurrentIndex];
updateQuestion();
}
});
mCheatButton = (Button)findViewById(R.id.cheat_button);
mCheatButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(QuizActivity.this,CheatActivity.class);
boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
i.putExtra(CheatActivity.EXTRA_ANSWER_IS_TRUE, answerIsTrue);
startActivityForResult(i,0);
}
});
if (savedInstanceState != null) {
mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);
mIsCheater = savedInstanceState.getBoolean(CHEATER_BOOLEAN, false);
}
updateQuestion();
} // End of onCreate(Bundle) method
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.d(TAG,"onActivityResult called");
if (data == null) {
return;
}
mIsCheater = data.getBooleanExtra(CheatActivity.EXTRA_ANSWER_SHOWN, false);
mCheatBank[mCurrentIndex] = mIsCheater;
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Log.d(TAG, "onSaveInstanceState");
savedInstanceState.putInt(KEY_INDEX, mCurrentIndex);
savedInstanceState.putBoolean(CHEATER_BOOLEAN, mIsCheater);
}
Edit 1: Here is the code from the other classes because someone asked.
CheatActivity Code:
public class CheatActivity extends Activity {
public static final String EXTRA_ANSWER_IS_TRUE = "com.bignerrandch.android.geoquiz.answer_is_true";
public static final String EXTRA_ANSWER_SHOWN = "com.bignerdranch.android.geoquiz.answer_shown";
private boolean mAnswerIsTrue;
private boolean mCheater;
private TextView mAnswerTextView;
private Button mShowAnswer;
private void setAnswerShownResult(boolean isAnswerShown) {
Intent data = new Intent();
data.putExtra(EXTRA_ANSWER_SHOWN, isAnswerShown);
setResult(RESULT_OK, data);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cheat);
mAnswerIsTrue = getIntent().getBooleanExtra(EXTRA_ANSWER_IS_TRUE, false);
mAnswerTextView = (TextView)findViewById(R.id.answerTextView);
mCheater = false;
if (savedInstanceState != null) {
mCheater = savedInstanceState.getBoolean(EXTRA_ANSWER_SHOWN, false);
setAnswerShownResult(mCheater);
if (mAnswerIsTrue) {
mAnswerTextView.setText(R.string.true_button);
}
else {
mAnswerTextView.setText(R.string.false_button);
}
} else {
setAnswerShownResult(false);
}
mShowAnswer = (Button)findViewById(R.id.showAnswerButton);
mShowAnswer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mAnswerIsTrue) {
mAnswerTextView.setText(R.string.true_button);
}
else {
mAnswerTextView.setText(R.string.false_button);
}
setAnswerShownResult(true);
mCheater = true;
}
});
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean(EXTRA_ANSWER_SHOWN, mCheater);
}
}
TrueFalse Code:
public class TrueFalse{
private int mQuestion;
private boolean mTrueQuestion;
public TrueFalse(int question, boolean trueQuestion) {
mQuestion = question;
mTrueQuestion = trueQuestion;
}
public int getQuestion() {
return mQuestion;
}
public void setQuestion(int question) {
mQuestion = question;
}
public boolean isTrueQuestion() {
return mTrueQuestion;
}
public void setTrueQuestion(boolean trueQuestion) {
mTrueQuestion = trueQuestion;
}
}
It's not entirely clear what you mean by wiped. But I think your problem is that you are using a member variable to set your array values. So you are setting each element of your array to that value. So essentially the entire array will be set to what ever the final state of mIsCheater is.
i want to change UI in android.
my main class make a second class then second class call a method of main class.method in main class should update UI but program crash in runtime.
what should i do?
my main class :
public class FileObserverActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
tv.setText("new world");
MyFileObserver myFileObserver = new MyFileObserver("/sdcard/", this);
myFileObserver.startWatching();
}
String mySTR = "";
TextView tv ;
public void event(String absolutePath,String path,int event)
{
mySTR = absolutePath+path+"\t"+event;
tv.setText(mySTR); // program crash here!
}
}
and my second class :
public class MyFileObserver extends FileObserver
{
public String absolutePath;
FileObserverActivity fileobserveractivity;
public MyFileObserver(String path,FileObserverActivity foa)
{
super(path, FileObserver.ALL_EVENTS);
absolutePath = path;
fileobserveractivity = foa;
}
#Override
public void onEvent(int event, String path)
{
if (path == null)
{
return;
}
else if(event!=0)
{
fileobserveractivity.event(absolutePath, path, event);
}
else
{
return;
}
}
}
You can't call UI methods from threads other than the main thread. You should use Activity#runOnUiThread() method.
public class FileObserverActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
tv.setText("new world");
MyFileObserver myFileObserver = new MyFileObserver("/sdcard/", this);
myFileObserver.startWatching();
}
String mySTR = "";
TextView tv ;
public void event(String absolutePath,String path,int event)
{
runOnUiThread(action);
}
private Runnable action = new Runnable() {
#Override
public void run() {
mySTR = absolutePath+path+"\t"+event;
tv.setText(mySTR);
}
};
}
public class MyFileObserver extends FileObserver
{
public String absolutePath;
FileObserverActivity fileobserveractivity;
public MyFileObserver(String path,FileObserverActivity foa)
{
super(path, FileObserver.ALL_EVENTS);
absolutePath = path;
fileobserveractivity = foa;
}
#Override
public void onEvent(int event, String path)
{
if (path == null)
{
return;
}
else if(event!=0)
{
fileobserveractivity.event(absolutePath, path, event);
}
else
{
return;
}
}
}
Try as follows
public class FileObserverActivity extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
tv.setText("new world");
MyFileObserver myFileObserver = new MyFileObserver("/sdcard/", this);
myFileObserver.startWatching();
registerReceiver(onBroadcast,new IntentFilter("abcd"));
}
String mySTR = "";
TextView tv ;
public void event(String absolutePath,String path,int event)
{
mySTR = absolutePath+path+"\t"+event;
tv.setText(mySTR); // program crash here!
}
#Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(onBroadcast);
}
private final BroadcastReceiver onBroadcast = new BroadcastReceiver() {
#Override
public void onReceive(Context ctxt, Intent i) {
// do stuff to the UI
event(absolutePath, path, event);
}
};
}
public class MyFileObserver extends FileObserver
{
public String absolutePath;
FileObserverActivity fileobserveractivity;
public MyFileObserver(String path,FileObserverActivity foa)
{
super(path, FileObserver.ALL_EVENTS);
absolutePath = path;
fileobserveractivity = foa;
}
#Override
public void onEvent(int event, String path)
{
if (path == null)
{
return;
}
else if(event!=0)
{
context.sendBroadcast(new Intent("abcd"));
//fileobserveractivity.event(absolutePath, path, event);
}
else
{
return;
}
}
}