Android app error “Unfortunately App has Stopped” - android

Hello i have a problem with running the application, when ill run it, its opening but when i will press the button saying me that the application is stopped and cant run: “Unfortunately, App has Stopped”. Here is my simple code and i hope i will get the answer fast...
Thanks allot :)
package com.stefan.stefan1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView textView1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView1.setText("Hello Im Stefan !");
}
});
}
#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;
}
}
Hello again i have new problem i have added another label(textView2) and im trying to add again this line textView2 = (TextView) findViewById(R.id.textView2);
but giving me error for adding something in class "R.java"
please help .. :/

You forgot to initialize textView1.
So when you're doing :
textView1.setText("Hello Im Stefan !");
it throws a NullPointerException and stop your app.
If you defined your textview in the layout, add after setContentView
textView1 = (TextView) findViewById(R.id.idOfYourTextView);
P.S : You should always provide the stacktrace(logcat) when you have such an error.

Related

Android WebView returns weird error

I am trying to create a simple browser app. Here is my code:
package com.degstu.ultralightbrowser;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private Button button;
//#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#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);
}
//Code for "GO" button
public void sendURL(View view) {
TextView textURL;
textURL = (TextView) findViewById(R.id.textBoxURL);
WebView webView = new WebView(this);
webView.loadUrl(textURL.toString());
setContentView(webView);
}
}
Everything mostly works, however, when I press "GO" in the app, I see this.
There are no errors recognized in my code, and any help would be appreciated.
Apparently, the url you wanted to visit is not valid (it starts with "andorid.support.v7..." which is a string containing a concise, human-readable description of the TextView object). To get the text of TextView, you should use getText() to return the text the TextView is displaying.
webView.loadUrl(textURL.getText().toString());
First of all,avoid extends ActionBarActivity .Use Activity or AppCompatActivity.
WHY
Since the version 22.1.0, the class ActionBarActivity is deprecated. You should use AppCompatActivity ORActivity .
Please update your Code Like this
TextView textURL;
textURL = (TextView) findViewById(R.id.textBoxURL);
WebView webView = new WebView(this);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(textURL.getText().toString());
For details you can visit here Android - WebView Tutorial
and https://developer.android.com/reference/android/webkit/WebView.html
I hope it helps you.

R.layout.activity_starting_point can not be resolved [duplicate]

This question already has answers here:
"R cannot be resolved to a variable"? [duplicate]
(30 answers)
Closed 8 years ago.
I just started coding in android and i am stuck at this point my code is simple its showing error in only these lines.
in the line setContentView(R.layout.activity_starting_point); its showing error that activity_starting_point can not be resolved or is not a field. why so please help!
package com.thenewboston.abhi;
import android.R;
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;
import android.widget.TextView;
public class StartingPoint extends ActionBarActivity {
int counter;
Button add, sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
counter=0;
add = (Button)findViewById(R.id.button1);
sub = (Button)findViewById(R.id.button2);
display = (TextView)findViewById(R.id.text1);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText("Your total is " + counter);
}
});
sub.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();
}
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.starting_point, 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() {
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_starting_point, container, false);
return rootView;
}
}
}
check your build target, delete import Android.R, clear your project, and check the error log for more information about what is causing your app to not generate the R class
Delete import android.R; as that is the Android API's R and not your project's one and is masking yours. You need your one as it contains your resources.
Try and see if auto-complete can find the target layout too. Type setContentView(R.layout. and see what options you are given. (You may need to invoke the auto-complete. It's Ctrl + Space on Eclipse. I don't know the shortcuts for Studio.)
Whenever you want to access the Android API's R.java you should fully qualify it with android.R.layout.whatever so it doesn't get auto-imported again and mess with your local resources.
If this doesn't work can you confirm that you have an R.java under the /gen/your/package/path folder? Perhaps try refreshing and rebuilding the project too.
If there are errors in your project R may not be able to successfully build. You should check your lint errors or other error logs and again refresh, rebuild, restart after sorting those.
Note that errors in your XML may not be picked up by eclipse so you should pay careful attention to those files. Just because the XML syntax is ok does not mean it is valid Android XML.

android public textbox

I have come across another prickly little problem.
I am writing an app with tabs, but I have a textbox (EditText) at the top of the screen that I want to be able to receive text data from any of the tabs. As each of the tabs has it's own activity and layout, this is proving difficult to achieve.
I want to be able to use:
editText1.setText("Hello World");//sample text
from any Tab/Activity.
Does anyone know how to make a textbox from one layout public and able to recieve text?
I am using TabActivity, yes I know it's deprecated but as this is my first app with tabs, it's easier to learn than Fragments. I will try them next time, unless they are the answer to my problem, in which case I have a lot of re-coding to do!!
ok, new part.
package com.epsilonitsystems.epecsandroid;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;
import android.view.Menu;
import android.speech.tts.TextToSpeech;
public class MainActivity extends TabActivity {
public EditText editText1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1);
TabHost tabHost = getTabHost();
// Tab for Core
TabSpec corespec = tabHost.newTabSpec("Core");
corespec.setIndicator("", getResources().getDrawable(R.drawable.ic_i));
Intent coreIntent = new Intent(this, CoreActivity.class);
corespec.setContent(coreIntent);
// Tab for Drink
TabSpec drinkspec = tabHost.newTabSpec("Drink");
drinkspec.setIndicator("", getResources().getDrawable(R.drawable.ic_drink));
Intent drinkIntent = new Intent(this, DrinkActivity.class);
drinkspec.setContent(drinkIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(corespec); // Adding Core tab
tabHost.addTab(drinkspec); // Adding Drink tab
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
That's the Main Activity, I'll just show the Core Activity as they will all be the same when I get it working.
package com.epsilonitsystems.epecsandroid;
import android.app.Activity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.speech.tts.TextToSpeech;
public class CoreActivity extends Activity {
private TextToSpeech mTts;
// This code can be any value you want, its just a checksum.
private static final int MY_DATA_CHECK_CODE = 1234;
EditText editText1;
String Spch,Str;
ImageButton imageButton1,imageButton2;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.core_layout);
//button1 = (Button) findViewById(R.id.button1);
editText1 = (EditText) findViewById(R.id.editText1);
mTts = new TextToSpeech(this, null);
}
public void onimageButton1Click(View view) {
//mTts.speak("I",TextToSpeech.QUEUE_FLUSH,null);
//Spch=editText1.toString();
//Spch=Spch+"I ";
editText1.setText("Hello World");
}
}//Activity End
I can't post a screenshot as I'm still a new user, sorry.
Any ideas please?
Gary,
What you are looking for probably is how to share data between different Activities.
You can do this in a couple of ways, but the cleanest is using the Intent you start the new Activity with.
With this you can set extra data via the Intent.putXXXXX methods.
You could use something as Intent.putExtra("my_private_key", mTextVar); and fetch that out in your other Activity with this.getIntent().getStringExtra("my_private_key");
ps. As tempting as it might seem to start with the TabActivity, you're actually making it a lot more difficult for yourself by making a bad start and learning classes which you should not use anymore. I'd advise you to pick up some good fragments tutorials which will be just as easy once correctly explained. You should take a look at per example http://www.vogella.com/android.html

Android app force closes soon as i open it on emulator

I went through similar kind of questions on older posts.. rectified them. Still getting force close problem. Please help.
Which of the following is correct?
Button continueButton = (Button) findViewById(R.id.continue_button);
or
View continueButton = findViewById(R.id.continue_button);
And what's the difference between them ?
package org.example.sudoku;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
public class Sudoku extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button continueButton = (Button) findViewById(R.id.continue_button);
continueButton.setOnClickListener((OnClickListener) this);
Button aboutButton = (Button)findViewById(R.id.about_button);
aboutButton.setOnClickListener((OnClickListener) this);
Button newButton = (Button)findViewById(R.id.new_game_button);
newButton.setOnClickListener((OnClickListener) this);
Button exitButton = (Button)findViewById(R.id.exit_button);
exitButton.setOnClickListener((OnClickListener) this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Both is correct, because Button is a subclass of View. But you will typically have to use Button so you can add listeners and other stuff to it.
You should really have a look at the logcat output of you application. There you will find a stacktrace that will help you identifying the problem.
I think the problem comes from the way you set up your click listeners on your buttons. You use this but your activity class does not implement the interface. I advice you to do that instead:
View.OnClickListener clickHandler = new View.OnClickListener() {
public void onClick(View v) {
}
}
Button continueButton = (Button) findViewById(R.id.continue_button);
continueButton.setOnClickListener(clickHandler);+-

other way to make start up menu instead using button

i am thinking of doing a start up menu before going into the real page like the picture below.
i think i can do it with button menu. so, the pikpok, all-star, etc i made it using button widget. Is there any other way to make this?
anyway, when user click on all-star button, it appear to the next xml file. but then the activity seems not working out.
this is my code. this code shows the start up activity.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button button;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
setContentView(R.layout.about);
}
});
button = (Button) findViewById(R.id.signin);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
setContentView(R.layout.report);
}
});
}
}
report.xml and about.xml will have their own activity named ReportActivity and AboutActivity but it does not work.
is there a better way to the start up menu instead of using button. also it would be nice if the menu could have animation to it.
For rather complex "Buttons" you can define your own View by using layouts/images/other view
check
http://developer.android.com/guide/topics/ui/custom-components.html
for more information.
To call another activity, you have to define an activity class for that activity first, which calls the layout etc. On how to start another activity read
http://developer.android.com/guide/components/activities.html#StartingAnActivity

Categories

Resources