Cannot assign string from none one class to another - java

very noob in programming here, i have an activity (control) in android studio and a class (MesageSender) which extends AsyncTask ,
so i wanted to give a text in activity control via edittext and save that to a string in class messagesender and display it back to control in a textview , here is the code for my control (activity) and MessageSender (class)
below is code of control activity
public class controler extends AppCompatActivity {
public MessageSender mmessagesender = new MessageSender();
static EditText et;
Button bt;
static TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
setContentView(R.layout.activity_controler);
et= findViewById(R.id.editText);
bt = findViewById(R.id.button);
tv = findViewById(R.id.tv);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mmessagesender.ip = et.getText().toString();
}
});
}
}
Here ip is string from MessageSender class and et is edittext in controll activity
Below is code of MessageSender class
public class MessageSender extends AsyncTask<String,Void,Void> {
Socket s;
DataOutputStream dos;
PrintWriter pw;
String ip;
#Override
protected Void doInBackground(String... Voids) {
String message = Voids[0];
try{
s =new Socket(ip,123);
if(ip != null){
controler.tv.setText(ip);
}
else{
controler.tv.setText("no ip");
}
}
catch(IOException e) {
e.printStackTrace();
}
return null;
}
}
the problem was i cannot see the textview tv in control change to what i gave in edittext et
any help is appreciated , thanks :)

Related

Large txt file reading via button waiting ages

Hi, why this code working "onCreate" but don`t working in the button
Realy I use not more then 20Mb, but for testing I inserted 90Mb
public class MainActivity extends AppCompatActivity {
TextView textView; DataInputStream textFileStream = null;
public void readbigtext(){
try {
textFileStream = new DataInputStream(getAssets().open(String.format("test1.txt")));
} catch (IOException e) {e.printStackTrace();}
Scanner sc = new Scanner(textFileStream);
while (sc.hasNextLine()) {textView.append(sc.nextLine());}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bigbutton = (Button) findViewById(R.id.bigbutton);
textView = (TextView) findViewById(R.id.bigfile);
textView.append("test begin");
//90Mb here IS WORKING!
// readbigtext();
bigbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//90Mb here DON`T WORKING
readbigtext();
}
});
}
}

How to Open a link on button click From a Default Activity other than Main Activity in Android Studio?

I am trying to assign a link to a button that is in another activity other than main activity. I tried using Intent and Uri.parse but the application kept crashing. On the other hand, if I used Intent and Uri.pase on a button in Main Activity it was working fine. Can anybody help me, please?
Here's the MainAcitivity and on "info" button press it takes me to a new activity.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button info;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
info = (Button) findViewById(R.id.info);
info.setOnClickListener(this);
SectionsPagerAdapter sectionsPagerAdapter = new
SectionsPagerAdapter(this, getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
public void onClick(View view) {
if (view.getId() == R.id.info) {
Intent intent = new Intent(".Options");
startActivity(intent);
}
}
}
The code for the second activity goes as follows:
public class Options extends AppCompatActivity implements View.OnClickListener {
Button button;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
button=(Button)findViewById(R.id.button);
setContentView(R.layout.activity_options);`
}
public void onClick(View view) {
}
}
I want that a link opens when I click on the button from the Options Activity.
You want to pass a Url from MainActivity to OtherActivity so that the user will be redirect to it when it will taps on OtherActivity.Button ?
If yes, use Intent to pass the Url
class OtherActivity : AppCompatActivity(){
companion object {
private const val EXTRA_KEY_URL = "OTHER_DETAIL.EXTRA_KEY_URL"
fun launch(launcher: Activity, url: String) {
val intent = Intent(launcher, OtherActivity::class.java)
.apply {
putExtra(EXTRA_KEY_URL, url)
}
launcher.startActivity(intent)
}
}
private lateinit var url: String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.other_activity)
url = intent.getStringExtra("EXTRA_KEY_URL")
myButton.setOnClickListener {
// use url here
}
}
}
How to call it from your MainActivity
val uri = /* your Uri */
val str = uri.toString()
OtherActivity.launch(this, str)
Edit according to your need, in Java
public class Options extends AppCompatActivity implements View.OnClickListener {
private static final String EXTRA_KEY_URL = "OTHER_DETAIL.EXTRA_KEY_URL";
Button button;
String url;
public static void start(final Activity launcher, String url) {
Intent intent = new Intent(launcher, Options.class);
intent.putExtra(EXTRA_KEY_URL, url)
launcher.startActivity(intent);
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_options);
url = getIntent().getStringExtra("EXTRA_KEY_URL")
button=(Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
public void onClick(View view) {
/* use url*/
}
}
Then, in MainActivity
public void onClick(View view) {
if (view.getId() == R.id.info) {
// get your url
String url = ""; //TODO
OtherActivity.launch(this, url);
}
}

Url input stream not showing

I'm trying to get input stream from URL to be displayed on TextView when clicked on a certain button in my android app. I used someone else's code from internet tutorial step by step but it doesn't work for me. The TextView is always empty even though it should already show the text. I would deeply appreciate any help.
public class fetchData extends AsyncTask<Void, Void, Void> {
String data="";
#Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("https://api.myjson.com/bins/j5f6b");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while(line != null){
line = bufferedReader.readLine();
data = data + line;
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
CurrencyConvert.text.setText(this.data);
}
}
public class CurrencyConvert extends AppCompatActivity {
Button click;
public static TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_currency_convert);
click = (Button) findViewById(R.id.button);
text = (TextView) findViewById(R.id.fetchText);
// data.setText("");
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fetchData process = new fetchData();
process.execute();
}
});
}
}
You shouldn't declare TextView as static varible of Activity class. Instead pass activity instance to fetchData class constructor (btw. class names should start with uppercase)
public class FetchData extends AsyncTask<Void, Void, Void> {
CurrencyConvert activity;
public FetchData(CurrencyConvert activity) {
this.activity= activity;
}
#Override
protected Void doInBackground(Void... arg0) {
// do background stuff...
}
#Override
protected void onPostExecute(Void result) {
activity.setTextInTextView(this.data);
}
In Your Activity declare public method for setting text:
public class CurrencyConvert extends AppCompatActivity {
Button click;
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_currency_convert);
click = (Button) findViewById(R.id.button);
text = (TextView) findViewById(R.id.fetchText);
data.setText("");
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fetchData process = new fetchData(this);
process.execute();
}
});
}
void setTextInTextView(String data) {
text.setText(data);
}
}

Using NumberPicker to change TextView on a different activity [android]

I'm trying to change a textview value on one activity by using a numberpicker on the previous activity. Any help would be appreciated.
Here's the relevant part of my Java from activity1
public class activity_game extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
final NumberPicker fizzNumberPkr = (NumberPicker)findViewById(fizzNumberPicker);
fizzNumberPkr.setValue(3);
fizzNumberPkr.setMinValue(1);
fizzNumberPkr.setMaxValue(20);
fizzNumberPkr.setWrapSelectorWheel(true);
final NumberPicker buzzNumberPkr = (NumberPicker)findViewById(buzzNumberPicker);
buzzNumberPkr.setValue(5);
buzzNumberPkr.setMinValue(1);
buzzNumberPkr.setMaxValue(20);
buzzNumberPkr.setWrapSelectorWheel(true);
}
public void toActivityPlay (View view) {
Intent toActivityPlay = new Intent(this, activity_play.class);
toActivityPlay.putExtra("fizzNumber", fizzNumber);
toActivityPlay.putExtra("buzzNumber", buzzNumber);
startActivity(toActivityPlay);
}
And my relevant java from activity2
public class activity_play extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
TextView fizzNumberText = (TextView)findViewById(R.id.fizzNumber);
fizzNumberText.setText(getIntent().getExtras().getString("fizzNumber"));
if(getIntent().hasExtra("fizzNumber")) {
fizzNumber = getIntent().getIntArrayExtra();
} else {
throw new IllegalArgumentException("Error: Fizz number not found");
}
If there's any relevant code that I may not have posted please let me know and i'll edit my post.
Some ideas come to me now to communicate two activities..
You can create a method setValuePicker()/getValueFromPicker() in the first Activity, and call getValue() in the other activity.
public void setValuePicker(Parameter value){
number = value;
}
public Parameter getValuePicker(){
return value;
}
If you variable have been set global only need implement getValue() method.
If you someday want use fragments need implement callback.
EDIT:
Using your code is something like this:
- FirstActivity
public class MainActivity extends AppCompatActivity {
private int number;
public static final String FIZZ_TAG = "fizz_numer";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final NumberPicker picker = (NumberPicker) findViewById(R.id.numberPicker);
picker.setMinValue(0);
picker.setMaxValue(10);
picker.setValue(5);
picker.setWrapSelectorWheel(true);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
number = picker.getValue();
Intent intent = new Intent(getApplicationContext(), Main2Activity.class)
.putExtra(FIZZ_TAG,number);
startActivity(intent);
}
});
}
}
Second Activity need be something like this.
.
.
.
.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
int s = getIntent().getIntExtra(MainActivity.FIZZ_TAG,0);
TextView textView = (TextView) findViewById(R.id.text_test);
textView.setText(String.format("%s = %d","number ",s));
}

Using another MainActivity? does it work?

Is it okay to do this? Creating another MainAcitivity since my original MainAcitivity is tied on the v4.view.pager activity_main. Although, this code doesn't post errors, this class seems to not be reflecting on the output. I'm using a tab pager adapter. Do I need to refresh or something? (It doesn't do anything that even the clickAction on this class doesn't work.
public class MainActivity2 extends Activity {
String url = "http://blog.compassion.com/living-in-the-philippines-rural-life/";
ProgressDialog mProgressDialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home_fragment);
TextView titlebutton = (TextView) findViewById(R.id.TextView01);
titlebutton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Execute Title AsyncTask
new Title().execute();
}
});
}
private class Title extends AsyncTask<Void, Void, Void> {
String body;
String title;
#Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity2.this);
mProgressDialog.setTitle("Android Basic JSoup Tutorial");
mProgressDialog.setMessage("Loading...");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
#Override
protected Void doInBackground(Void... params) {
try {
Document document = Jsoup.connect(url).get();
Elements elements = document.select("p");
title = document.title();
for(Element elements123 : elements){
body+=elements123.text();
System.out.println(elements123.text());
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Void result) {
TextView txttbody = (TextView) findViewById(R.id.textView2);
txttbody.setMovementMethod(new ScrollingMovementMethod());
txttbody.setText(title +"\n"+body);
mProgressDialog.dismiss();
}
}
}

Categories

Resources