Unfortunately myfirstproject stopped working error in java - android

I know this is a very simple question for you experts,but please forgive me.Im very new to this android platform.
I tried to display a string in a textview when a button is clicked.
But it is not working.No errors are showing.
Can you please help me with where im going wrong.
Thanks in advace.
Below is the code i wrote:
package com.mycmpny.namebtn;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
public class NameonbuttonclickActivity extends Activity implements View.OnClickListener {
Button mybtn;
TextView txtView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mybtn= new Button(this);
mybtn.setOnClickListener(this);
printmyname();
setContentView(mybtn);
// setContentView(R.layout.main);
}
public void onClick(View view){
printmyname();
}
private void printmyname(){
txtView.setText("This is my first app");
}
}

I have done some modifications in onCreate().here i am making a textview which you left.Do that and say is it working ?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mybtn= new Button(this);
mybtn.setOnClickListener(this);
txtView=new TextView(this);
// printmyname();
LinearLayout ll=new LinearLayOut(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.addView(mybtn);
ll.addView(txtView);
setContentView(ll);
// setContentView(R.layout.main);
}
HTH :)

You have not created a new object of TextView.
txtView = new TextView(this);
Else use findViewById if you've created the text View in your xml files.
txtView = (TextView)findViewById(R.id.TextView1);

Related

This is the main activity of app. But this code somehow crashes my app. What changes should be made in order to make it work

I'm learning how to make UI programatically in Android. I'm trying to make an app with just one button, which when clicked, a toast pops up saying how many times the button has been clicked.
package com.example.android.dynamicui;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatButton;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private int mCount=0;
float den = getResources().getDisplayMetrics().density;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Linear Layout
LinearLayout mLayout = new LinearLayout(this);
mLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
mLayout.setOrientation(LinearLayout.VERTICAL);
mLayout.setGravity(Gravity.CENTER);
//Button
AppCompatButton mButton = new AppCompatButton(this);
mButton.setText("Click me!");
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
buttonParams.setMargins(0,(int)(15*den + 0.5f),0,0);
mButton.setLayoutParams(buttonParams);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCount++;
Toast.makeText(getBaseContext(), "You pressed button for "+mCount+" times!" , Toast.LENGTH_SHORT ).show();
}
});
//Adding button to Linear Layout
mLayout.addView(mButton,buttonParams);
setContentView(mLayout);
}
}
Please set the on click on button after the layout has been set i.e.:-
public class MainActivity extends AppCompatActivity {
private int mCount=0;
float den = getResources().getDisplayMetrics().density;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Linear Layout
LinearLayout mLayout = new LinearLayout(this);
mLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
mLayout.setOrientation(LinearLayout.VERTICAL);
mLayout.setGravity(Gravity.CENTER);
//Button
AppCompatButton mButton = new AppCompatButton(this);
mButton.setText("Click me!");
LinearLayout.LayoutParams buttonParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
buttonParams.setMargins(0,(int)(15*den + 0.5f),0,0);
mButton.setLayoutParams(buttonParams);
//Adding button to Linear Layout
mLayout.addView(mButton,buttonParams);
setContentView(mLayout);
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mCount++;
Toast.makeText(getBaseContext(), "You pressed button for "+mCount+" times!" , Toast.LENGTH_SHORT ).show();
}
});
}
}
It was crashing because I was using getResources() method, before onCreate(), which passed a null value to the 'den' variable.

android hiding ui and showing ui code not working [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 6 years ago.
package com.example.eiraj.listviewseefrgmentsfiirst;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView textView= (TextView) findViewById(R.id.textView);
public void show(View view){
textView.setVisibility(View.VISIBLE);
}
public void hide(View view){
textView.setVisibility(View.INVISIBLE);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
here two buttons are being used to show data and hide data but whenever I try to run it emulator shows the message unfortunately uihide stopped also I downloaded the apk to run it on my phone but same message came there
Move TextView textView= (TextView) findViewById(R.id.textView); inside onCreate
while keeping textView reference outside onCreate because before the execution of onCreate there is no layout attached to your activity , hence the issue
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= (TextView) findViewById(R.id.textView);
}
Then your MainActivity.java will be
public class MainActivity extends AppCompatActivity {
TextView textView;
public void show(View view){
textView.setVisibility(View.VISIBLE);
}
public void hide(View view){
textView.setVisibility(View.INVISIBLE);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= (TextView) findViewById(R.id.textView);
}
}
Plus you can also use textView.setVisibility(View.GONE); if you want to completely make your view invisible
Attach your TextView inside onCreate():
package com.example.eiraj.listviewseefrgmentsfiirst;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= (TextView) findViewById(R.id.textView);
}
public void show(View view){
textView.setVisibility(View.VISIBLE);
}
public void hide(View view){
textView.setVisibility(View.INVISIBLE);
}
}
We can't initialize Views before setContentView() is called and it is compulsory to call setContentView() inside onCreate() method.
Do like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView= (TextView) findViewById(R.id.textView);
}

Android generate new objects from onClick

I try to develop a simple Android App with one Button which generates new TextViews on each click.
import android.app.Activity;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CreateTV extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mCreate = (Button)findViewById(R.id.btnCreate);
mCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
((Button) v).setText("Clicked");
TextView mTV1 = new TextView(this);
}
});
}
}
My code is wrong because of:
TextView mTV1 = new TextView(this);
I could find some similar examples, which generate objects programmatically in onCreate(). But I want to generate and modify new objects in onClick().
Would anybody please help?
Change
TextView mTV1 = new TextView(this);
to
TextView mTV1 = new TextView(CreateTV.this);
Views can only be instantiated with a context as parameter
As you can see in the documentation a TextView needs the context to be created. TextView(Context context)
Since you are trying to create a TextView inside a ClickListener you can not use this as a reference to a Context-extending object.
As McAdam331 pointed out, use new TextView(getActivity), this works because Activity extends Context.
In addition to change TextView mTV1 = new TextView(this); to TextView mTV1 = new TextView(CreateTV.this);, you must add the TextView within a view like the following:
import android.app.Activity;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class CreateTV extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mCreate = (Button)findViewById(R.id.btnCreate);
mCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
((Button) v).setText("Clicked");
TextView mTV1 = new TextView(CreateTV.this);
addContentView(mTV1);
}
});
}
}
I would prefer adding a Context, setting it to final and then call the Textview using the Context.
Example:
public class CreateTV extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mCreate = (Button)findViewById(R.id.btnCreate);
final Context mContext = this;
mCreate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
((Button) v).setText("Clicked");
TextView mTV1 = new TextView(mContext);
addContentView(mTV1);
}
});
}
}
If you want to use the Context outside the onCreate method (and within Listeners) you can define a Context.
private Context context;
public void onCreate(....) {
this.context = this;
}
private void aMethod() {
context....
}
Theres another way doing such cool stuff. Create a Class and extends it by Application.
public class MainApplication extends Application {
public static Context getContext() { return this; }
}
Then add the MainApplication to your Manifest.
<application
android:name=".MainApplication"
>
and access it from everywhere with MainApplication.getContext();

Android with Eclipse Buttons

I'm just starting out with the Android/Eclipse SDK and I have no previous Java experience.
I've seen lots of tutorials for running Toasts onclick but I'm trying to make it so that when a button is clicked a text field is populated with the text of that button.
In other words if I press a button which is labelled as 'Hello' then the contents of a textfield will become 'Hello'.
Any help is greatly appreciated.
TextView mText=(TextView)findViewById(R.id.textview1);
Button mbutton=(Button)findViewById(R.id.button1);
mbutton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
mText.setText("Hello");
}
});
Hope i may works..
Wrapping it up. You must have a "button1" and a "textview1" defined in your main.xml.
package my.dummy.hello;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.View.OnClickListener;
public class HelloActivity extends Activity implements OnClickListener {
Button b=null;
TextView tv=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(Button)findViewById(R.id.button1); // button1 set in main.xml
b.setOnClickListener(this);
tv=(TextView)findViewById(R.id.textview1); // textview1 set in main.xml
}
public void onClick( View v ) {
if (v == b) {
tv.setText( b.getText() );
}
}
}
write code in onClick()
String text = view.getText();
// use toast here to display text..
Suppose your button's id is button1, and textview's id is textview1
Button My_Button=(Button)findViewById(R.id.button1);
TextView textView = (TextView)findViewById(R.id.textview1);
My_Button.setOnClickListener(new OnClickListener(){
public void onclick(View v){
textView.setText("hello");
}
);

Android Setting text from a getter method in another class

I've looked all over the place and taken all of the tips given, but my app still force closes.
The app has two classes (Main and Next) and "Next" class has a get method. When I try to use the get method in the "Main" class my app force closes. This is the line of code that causes the problems:
timesHit_txtview.setText(next.getTimesHit());
Like I mentioned before, I don't get any errors in eclipse. The First error I get from log cat is "Uncaught handler: thread main exiting due to uncaught exception"
Any suggestions? Thanks in advance
EDIT:
package com.whatever.main;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Next next = new Next();
TextView timesHit_txtview = (TextView) findViewById(R.id.textView2);
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timesHit_txtview.setText(5);
Button next_btn = (Button) findViewById(R.id.button1);
next_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent nextscreen_intent = new Intent(view.getContext(), Next.class);
startActivityForResult(nextscreen_intent, 0);
}
});
}
}
You're trying to initialize a widget before the onCreate() call has been completed.
Initialize your TextView in the onCreate() method before you call setText on it, like so:
public class MainActivity extends Activity {
Next next;
TextView timesHit_txtview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
next = new Next(); // Initialize Next
timesHit_txtview = (TextView) findViewById(R.id.textView2); // Initialize Widget
timesHit_txtview.setText(5);
Button next_btn = (Button) findViewById(R.id.button1);
next_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent nextscreen_intent = new Intent(view.getContext(), Next.class);
startActivityForResult(nextscreen_intent, 0);
}
});
}
In your current code, timesHit_txtview is null when you try to set a text for it, which makes you Activity crash.

Categories

Resources