Why dont my app stop working? - android

I tried to run this simple code on emulator but it gives error right at the beginning. Helps appreciated. It gives a fatal error in logcat. It says "application has stopped". I get these errors:
"03-22 17:43:53.315: E/AndroidRuntime(1283): Process: com.example.deneme, PID: 1283"
"03-22 17:43:53.315: E/AndroidRuntime(1283): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.deneme/com.example.deneme.MainActivity}: java.lang.NullPointerException"
package com.example.deneme;
import android.app.Activity;
import android.app.ActionBar;
import android.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.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class MainActivity extends Activity {
final Button button = (Button) findViewById(R.id.button1);
EditText txt = (EditText) findViewById (R.id.editText1);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
txt.setText("murat");
}
});
// 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 are assigning values to button and txt before the view has been created in onCreate(). You shouldn't assign them until after the call to setContentView(R.layout.activity_main).

You need to put button and textview in onCreate.

Try the following
public class MainActivity extends Activity {
final Button button; // Don't call find view by ID here;
EditText txt; // Don't call find view by ID here;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt = (EditText) findViewById (R.id.editText1); // Instead call it after setContentView
button = (Button) findViewById(R.id.button1); // Instead call it after setContentView
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
txt.setText("murat");
}
});
// 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;
}
}
}

These lines will give you errors:
final Button button = (Button) findViewById(R.id.button1);
EditText txt = (EditText) findViewById (R.id.editText1);
You cannot initialize the Button or the EditText outside of any methods because the layout has not been set. You can, however, declare them outside of any methods and then initialize them inside of your onCreate method just after you have set the layout (the view). Like so:
// declare them at a class level
Button button;
EditText txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize them here
button = (Button) findViewById(R.id.button1);
txt = (EditText) findViewById (R.id.editText1);
button.setOnClickListener(new View.OnClickListener() {
...
Then button cannot be final, though.

Related

setText and OnClickListener does not work [duplicate]

This question already has answers here:
NullPointerException accessing views in onCreate()
(13 answers)
Closed 8 years ago.
I am new in Android so i am facing many problems. I am to do a simple program which can take a value from edittext and print it in the textview by clicking on a button. Here is my code:
The program says, unfortunately it's stopped working. But when ever i delete last few lines from setOnClickListener it works fine. also i have tested it only by setText. it also is not working. thanks.
Logcat:
XML:
Your code should be in PlaceHolderFragment, like following..
public class PlaceHolderFragment extends Fragment {
private EditText et;
private Button b;
private TextView tv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
et=(EditText) getView().findViewById(R.id.EditText1);
b = (Button) getView().findViewById(R.id.Button1);
tv = (Button) getView().findViewById(R.id.textView3);
b.setOnClicklistener(new OnClickListener(){
#Override
public void onClick(View arg0){
tv.setText("Pressed");
}
});
}
}
And remove these codes from onCreate()
et=(EditText) findViewById(R.id.EditText1);
b = (Button) findViewById(R.id.Button1);
tv = (Button) findViewById(R.id.textView3);
b.setOnClicklistener(new OnClickListener(){
#Override
public void onClick(View arg0){
tv.setText("Pressed");
}
});
Try this...
Update
activity_main.xml
fragment_main.xml
MainActivity.java
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBarActivity;
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.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
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();
}
}
#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 {
private EditText et;
private Button b;
private TextView tv;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
et = (EditText) getView().findViewById(R.id.editText1);
b = (Button) getView().findViewById(R.id.button1);
tv = (TextView) getView().findViewById(R.id.textView1);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
tv.setText("Pressed");
}
});
}
}
}
Output:
I think the xml you shared is fragment_main not activity_main. Make sure you are inflating the right xml and refer to it's control only... The NULL Pointer Exception is being generated due to that only.
As per your code, the textview , edittext and button must reside in activity_main.xml only
et=(EditText) findViewById(R.id.EditText1);
b = (Button) findViewById(R.id.Button1);
tv = (TextView) findViewById(R.id.textView3);
Confirm it.
If the control are in frgament_main, you need to use this code :
public class PlaceHolderFragment extends Fragment {
private EditText et;
private Button b;
private TextView tv;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
et=(EditText) rootView.findViewById(R.id.EditText1);
b = (Button) rootView.findViewById(R.id.Button1);
tv = (TextView) rootView.findViewById(R.id.textView3);
b.setOnClicklistener(new OnClickListener(){
#Override
public void onClick(View arg0){
tv.setText("Pressed");
}
});
return rootView;
}
}

Transferring code from ActivityBased UI to fragment based UI

I am getting a null pointer exception at line 39 due to the text view being created in fragment.xml and its code being added in main activity class. What shall I do to the highlighted piece of code to make it work in the same way, but in the PlaceHolderFragment class?
package com.example.myfirstapp;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
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.widget.DatePicker;
import android.widget.TextView;
import android.os.Build;
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* setContentView(R.layout.activity_display_message);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
} */
getSupportActionBar().show();
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
***TextView textView = (TextView)findViewById(R.id.textView1);
textView.setTextSize(40);
textView.setText(message);***
// Set the text view as the activity layout
setContentView(R.layout.fragment_display_message);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, 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_display_message,
container, false);
return rootView;
}
}
}
The inflation of the textview should be inside the fragment class if it was added in the fragment layout(same as activity). So move the textview inside the onCreateView() method of the fragment. It should look something like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_calendar, container,
false);
TextView textView = (TextView)view.findViewById(R.id.textView1);//make this a class variable
textView.setTextSize(40);//put this method in onStart()
textView.setText(message);//put this method in onStart()
return view;
}
hi i think you should write
// Create the text view
TextView textView = (TextView)findViewById(R.id.textView1);
textView.setTextSize(40);
textView.setText(message);
below
setContentView(R.layout.fragment_display_message);

Error on create a Button Listener

My intent is to get text from an EditText and view it in a Toast message. The code is this:
package com.example.primaapplicazione;
import android.app.Activity;
import android.app.ActionBar;
import android.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.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
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();
}
}
public void fromEditToToast()
{
EditText e = (EditText)findViewById(R.id.editText1);
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
}
});
}
#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;
}
}
}
If I call the method "fromEditToToast()" in onCreate() without the "setOnClickListener()", it compiles properly and the app opens. If I call the same method with that function on the emulator app doesn't open, showing this message "Unfortunately, PrimaApplicazione has stopped.
What should I have to do?
I have seen this is a very common error, The PlaceholderFragment uses the fragment_main.xml that must contain the elements editText1 and button1, probably you have that elements defined in your activity_main.xml
It may be that views are not being instantiated while the activity has started. Device works faster than emulator so it might work in a device while not in emulator.
Implement your onClickListener directly in fragment and show the toast from there, it will work (assuming EditText and Button lies in fragment).
Try following code in your fragment and call the following function in your fragment's onViewCreated function to set the button's listener : -
private void set buttonOnClickListener()
{
EditText e = (EditText)getView().findViewById(R.id.editText1);
Button b = (Button)getView().findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//your code goes here;
}
});
}

Why "OnClickListener" is not working

I can't understand why this simple code is not working.
This code suppose to set the color of the button's text to blue, that's all.
I only added to a new created project the lines :
final Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Perform action on click
btn.setTextColor(Color.BLUE);
}
});
The full code :
package com.example.b;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Perform action on click
btn.setTextColor(Color.BLUE);
}
});
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;
}
}
}
Most likely the button is in the fragment and not the activity. If so, move your findViewById and your setonclick code into your PlaceHolderFragment.

Eclipse 4 errors

These are the error messages I get:
Cannot cast from an int toEditText
EXTRA_MESSAGE cannot be resolved to variable
PlaceholderFragment cannot be resolved to type
The nested type DisplayMessageActivity cannot hide an enclosing type
This is my code
package com.example.thirdapp;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
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();
}
}
#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;
}
/** Called when the user clicks the Send button */
public void sendMessage(View view) {
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) (R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}
}
You need to find the edit text view by id.
EditText editText = (EditText) view.findViewById(R.id.edit_message);
You need put the function "sendMessage" out of "PlaceholderFragment".
Here is mine:
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}

Categories

Resources