I know for a fact I'm missing something extremely small but i can not seem to get my code to compile without errors. I am attempting to get the text field to take in a decimal number and OnClick, it will spit out the numbers binary equivalent.
It keeps telling me to initialize Binary but if i set it to 0, that makes Binary '0' instead of converting the decimal number.
Any help will would great! This is is my MainActivity.java file:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
final EditText editDecimal = (EditText) findViewById(R.id.editDecimal);
final EditText editBinary = (EditText) findViewById(R.id.editBinary);
Button buttonConvert = (Button) findViewById(R.id.buttonConvert);
buttonConvert.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
int decimal = Integer.valueOf(editDecimal.getText().toString());
int binary;
editBinary.setText(Integer.toBinaryString(binary));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
You don't need the variable binary. Instead you just need to do this:
editBinary.setText(Integer.toBinaryString(decimal));
Related
So, When I try to launch this application it crashes. However, it only crashes when I try to use the setOnClickListener. If I comment out that small section it runs just fine.
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
final TextView textView = (TextView) findViewById(R.id.textView);
final Button button = (Button)findViewById(R.id.button);
super.onCreate(savedInstanceState);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//textView.setText("Boop!");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
OK Working code! I had to move super.onCreate up, and declair the textview inside the findviewbyID section. However, now it works as I intended. Thank you guys for the help! I just wanted to make something very basic for a date. xD you guys are a life saver.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Boop!");
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
WORKING
The error is coming because you are calling the super.onCreate(savedInstanceState);
after the setContentView and then using the variables.
Reason that the error is not coming until you don't write onClicklister:
The variables textView and button are already null or in inconsistent state, but when you are writing the click listener, the variables are used and you are facing error.
You declared textView as final:
A variable can be declared final. A final variable may only be assigned to once.
final TextView textView = (TextView) findViewById(R.id.textView);
so you cannot modify it.
You must get the view inside onclick method:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("Boop!");
}
});
Hello I recently updated may ADT and and now I am developing new app using eclipse. so i want to know where to write setOnclickListner event. I want to open another activity on click button. I tried various combinations but it's gives me error every time. my eclipse Version: 8.1.2.201302132326 please tell me where i can write code??????
after adding setContentView(R.layout.activity_main); it gives an Error and application will unfortunately stooped.
I also remove some code and run it but it does not worked.
eg.
if (savedInstanceState == null)
{
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
and
public static class PlaceholderFragment extends Fragment
{
public PlaceholderFragment()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
this code remove from coding but it's not worked.
and in that when i am creating new blank activity then there is fragment activity.xml will created automatically and I want make GUI changes in fragment activity.xml file. If I made changes in activity.xml then it gives error.
Please help me.
Thank you in advance..
here is may code which is automatically generated...
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null)
{
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
{
public PlaceholderFragment()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
you should put the button fragment container,
so,you need to access your button you should specify like this because your placehold fragment inflate the layout of fragment_container.xml
Button b1=(Button)rootView.findViewById(R.id.button1);
b1.setOnclickListener(this);
in this case implements the onClickLiistener your class,Automatically onclick will override in your class
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null)
{
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
{
public PlaceholderFragment()
{
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
final Activity contect=getActivity();
Button b1=(Button)rootView.findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in=new Intent(contect,YourActivity.class);
startActivity(in);
}
}) ;
return rootView;
}
}
}
put the button in fragment_main,move to the some other activity
Otherwise remove the fragment and extends the activity also remove the appcompat_v7 libraries from the code and project.
Remove the libraries Properties-->Android--> from below remove it
This question already has answers here:
NullPointerException accessing views in onCreate()
(13 answers)
Closed 8 years ago.
So I have This Code, now after i added a smiple action to the button add(setOnClickListener) when I run the app it immidiatley crashes...
any idea why?
public class MainActivity extends ActionBarActivity {
int counter;
Button add,sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button)findViewById(R.id.bAdd);
sub = (Button)findViewById(R.id.bSub);
display = (TextView)findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Your Total is " + counter);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
It's most likely that your line
add = (Button)findViewById(R.id.bAdd);
is returning null. Try verifying this by printing
Log.d("TEST", "is null: " + (add == null));
The next step is to ensure sure that Button with id "bAdd" is actually defined in your activity_main XML. Edit your question to add your XML file activity_main.
When trying to make an android app, I encountered the error:
No enclosing instance of the type MainActivity is accessible in scope on the line
Toast.makeText(MainActivity.this, "Swag", Toast.LENGTH_SHORT).show();
This is my MainActivity.java:
public class MainActivity extends ActionBarActivity {
static Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements View.OnClickListener {
public PlaceholderFragment() {
}
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Swag", Toast.LENGTH_SHORT).show();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,false);
btn=(Button) rootView.findViewById(R.id.test_button);
btn.setOnClickListener(this);
return rootView;
}
}
}
You must use getActivity() inside the fragment.
Be careful, if you are doing some background job, or any other async task when it return, getActivity() may be null. So every time you use it check for null.
Further information on fragments, you can use this.
New programmer here. I want the Add button on my screen to enable only when all fields are non-empty. However, adding a TextWatcher is making the screen crash. Asking elsewhere, it seems I shouldn't try to access my EditText with my ContentView set to activity_add_grade. But I need help understanding the right way to do things.
The template/tutorial I followed had me create 2 xml files per activity. As seen below, my AddClass sets the contentView to activity_add_grade and then inflates fragment_add_grade, which is where my screen design and views are. So the problem with accessing my EditText views from onCreate is that my ContentView is not set to the xml that contains the views. So where should I set my listeners instead? And why are there two xmls anyway? I'd like to understand more and follow the best practice. Thanks!
AddClass -- the java for the activity in question
public class AddClass extends ActionBarActivity {
DBAdapter db = new DBAdapter(this);
private EditText editText1;
private EditText editText2;
public TextWatcher watcher = new TextWatcher(){
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
checkFieldsForEmptyValues();
}
#Override
public void afterTextChanged(Editable editable) {
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_grade);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
// setContentView(R.layout.fragment_add_grade); //Tried this. Doesn't work
editText1 = (EditText)findViewById(R.id.editTitle);
editText2 = (EditText)findViewById(R.id.editCredit);
editText1.addTextChangedListener(watcher);
editText2.addTextChangedListener(watcher);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add_grade, menu);
//Code here for spinners on screen. Removed for space
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void checkFieldsForEmptyValues() {
Button b = (Button)findViewById(R.id.addClassButton);
System.out.println("in the check fields thing");
String s1 = editText1.getText().toString();
String s2 = editText2.getText().toString();
if(s1.isEmpty() || s2.isEmpty()) {
b.setEnabled(false);
} else {
b.setEnabled(true);
}
}
// THIS WORKS -- the method is bigger, but I shortened it for space.
public void createNewClass(View v) {
EditText titleTxt = (EditText)findViewById(R.id.editTitle);
//ToStrings
String titleStr = titleTxt.getText().toString();
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_add_grade,
container, false);
return rootView;
}
}
Well the view you are looking for is in your fragment's layout, so you should handle that view inside your fragment.
Option A:
remove all EditText-related code from your Activity onCreate method. Create fields watcher and editText1, editText2 inside the Fragment. So your PlaceholderFragment code should look more or less like this:
public static class PlaceholderFragment extends Fragment {
EditText editText1, editText2;
public TextWatcher watcher = new TextWatcher(){
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
checkFieldsForEmptyValues();
}
#Override
public void afterTextChanged(Editable editable) {
}
};
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
editText1 = (EditText) rootView.findViewById(R.id.editTitle);
editText1.addTextChangedListener(watcher);
editText2 = (EditText) rootView.findViewById(R.id.editCredit);
editText2.addTextChangedListener(watcher);
return rootView;
}
}
Option B:
Since it looks like your fragment is the autogenerated one from eclipse and you may not want to use fragments in your code you can do this:
Drop your fragment code & layout and move the edittexts from your fragment layout in to your activitiy layout. Then your onCreate code of the Activity should work.