Skip to content

Instantly share code, notes, and snippets.

@koalahamlet
Created January 31, 2014 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koalahamlet/8728054 to your computer and use it in GitHub Desktop.
Save koalahamlet/8728054 to your computer and use it in GitHub Desktop.
the getUritoVisit() method works fine when it is still inside the oncreate, but as soon as I move it out of the oncreate and into the activities main body, the edit text always comes back as null when I try to access it. Da fuq?
package com.example.testapplication;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class FifthActivity extends ActionBarActivity {
public EditText etText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fifth);
final EditText etText = (EditText) findViewById(R.id.editText);
Button secondButton = (Button) findViewById(R.id.button2);
secondButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(FifthActivity.this,"you clicked the second button",Toast.LENGTH_SHORT).show();
}
});
Button launchButton = (Button) findViewById(R.id.button3);
launchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri url = getUriToVisit();
if (url != null) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(url);
startActivity(i);
}
}
public Uri getUriToVisit() {
etText.getText().toString();
String urlAddress;
urlAddress = etText.getText().toString();
if (urlAddress != null) {
if (!urlAddress.startsWith("http://"))
urlAddress = "http://" + urlAddress;
return Uri.parse(urlAddress);
} else {
return null;
}
}
// this code pairs with that stuff in main activity
// Intent i = new Intent(FifthActivity.this, MainActivity.class);
// String sting = etText.getText().toString();
// i.putExtra("text", sting);
// startActivity(i);
});
}
public void firstButtonClicked(View v) {
// checking that shit works in the xml
System.out.println("whaddup");
Toast.makeText(this,"Hello",Toast.LENGTH_SHORT).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment