So I am working on an app and my app keeps stopping unexpectedly. Here is my code and manifest file:
MainActivity
package com.example.lasic;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
#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.main, menu);
return true;
}
public void goNext(View view) {
startActivity(new Intent(this,QuestionActivity.class));
}
}
QuestionActivity
package com.example.lasic;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class QuestionActivity extends Activity{
private int counter = 1;
private UserScanner scanner = new UserScanner();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firstq);
loadNext();
}
public void loadNext() {
TextView view1 = (TextView) findViewById(R.id.textView1);
view1.setText(scanner.questionList.get(counter));
Button button1 = (Button) findViewById(R.id.button1);
button1.setText(scanner.left.get(counter));
Button button2 = (Button) findViewById(R.id.button2);
button2.setText(scanner.centerLeft.get(counter));
Button button3 = (Button) findViewById(R.id.button3);
button3.setText(scanner.center.get(counter));
Button button4 = (Button) findViewById(R.id.button4);
button4.setText(scanner.centerRight.get(counter));
Button button5 = (Button) findViewById(R.id.button5);
button5.setText(scanner.right.get(counter));
counter = counter + 1;
}
}
manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lasic"
android:versionCode="1" android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:debuggable = "true"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.lasic.MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name="com.example.lasic.QuestionActivity"
android:label="EntrySurvey" >
</activity>
</application>
</manifest>
When I try to switch from MainActivity to QuestionActivity using goNext() method, I get "this app stopped unexpectedly error". How do I fix this please?
You Try This
in goNext method
{
Intent intent = new Intent(MainActivity.this ,QuestionActivity.class);
startActivity(intent);
}
And in manifest file just give
android:name=".QuestionActivity" is enough
I don't think that there is any error in this line:
startActivity(new Intent(this,QuestionActivity.class));
It is correct. Are you sure there is not any error in your layout file? For example when you are not define android:layout_width or android:layout_height for the button you may get "this app stopped unexpectedly error". So check your layout file.
Related
I have created an android application with a simple login page, when i try run the app though i keep getting the same error. Can someone please tell me what I am missing here? Below are my android manifest and my two activities java classes.
Main Activity (Login) -
package com.example.squashsademo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText mTextUsername;
EditText mTextPassword;
Button mButtonLogin;
TextView mTextViewRegister;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
mTextUsername = (EditText)findViewById(R.id.edittext_username);
mTextPassword = (EditText)findViewById(R.id.edittext_password);
mButtonLogin = (Button)findViewById(R.id.button_login);
mTextViewRegister.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent RegisterIntent = new Intent(MainActivity.this,Register_Activity.class);
startActivity(RegisterIntent);
}
});
mButtonLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, PlayerInfo.class));
}
});
}
}
Register Activity -
package com.example.squashsademo;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Register_Activity extends AppCompatActivity {
EditText mTextEmail;
EditText mTextUsername;
EditText mTextPassword;
EditText mTextCnfPassword;
Button mButtonRegister;
TextView mTextViewLogin;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_);
mTextUsername = (EditText)findViewById(R.id.edittext_username);
mTextPassword = (EditText)findViewById(R.id.edittext_password);
mTextCnfPassword = (EditText) findViewById(R.id.edittext_cnf_password);
mButtonRegister = (Button)findViewById(R.id.button_register);
mTextViewLogin = (TextView)findViewById(R.id.txtViewLogin);
mTextViewLogin.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent LoginIntent = new Intent(Register_Activity.this,MainActivity.class);
startActivity(LoginIntent);
}
});
}
}
Android Manifest -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.squashsademo">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.SquashSADemo">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<action android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".Register_Activity" />
<activity android:name=".PlayerInfo" />
</application>
</manifest>
You have to replace
<action android:name="android.intent.category.LAUNCHER" />
with
<category android:name="android.intent.category.LAUNCHER" />
in your AndroidManifest.xml file.
From first activity to second activity navigation is working but second activity to third activity navigation is not working in android can any1 please help me.
This is my MainActivity.java
package com.exampl.test;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.app.Activity;
import android.view.View;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void activity2(View view){
Intent intent = new Intent(this,com.exampl.test.MainActivity2.class);
startActivity(intent);
}
#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;
}
}
This is my MainActivity2.java
package com.exampl.test;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity2 extends Activity {
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
public void activity3(View view){
Intent intent = new Intent(this,com.exampl.test.MainActivity3.class);
startActivity(intent);
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
This is my MainActivity3.java
package com.exampl.test;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity3 extends Activity {
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity3);
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
This is my appmanifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exampl.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.exampl.test.MainActivity2"
android:label="#string/title_activity_main_activity2"
android:parentActivityName="com.exampl.test.MainActivity" >
</activity>
<activity
android:name="com.exampl.test.MainActivity3"
android:label="#string/title_activity_main_activity3"
android:parentActivityName="com.exampl.test.MainActivity2" >
</activity>
</application>
</manifest>
please help me wt to modify in above coding
Thanks in advance
Try This!
MainActivity
package com.example.acct;
import com.example.acct.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button =(Button) findViewById(R.id.button1);
Toast.makeText(this, "Activity1", Toast.LENGTH_SHORT).show();
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
activity2();
}
});
}
public void activity2() {
Intent intent = new Intent(this, com.example.acct.MainActivity2.class);
startActivity(intent);
}
#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;
}
}
MainActivity2
package com.example.acct;
import com.example.acct.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity2 extends Activity {
Button button;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getActionBar().setDisplayHomeAsUpEnabled(true);
button = (Button) findViewById(R.id.button1);
Toast.makeText(this, "Activity2", Toast.LENGTH_SHORT).show();
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
activity3();
}
});
}
public void activity3() {
Intent intent = new Intent(this, com.example.acct.MainActivity3.class);
startActivity(intent);
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
MainActivity3
package com.example.acct;
import com.example.acct.R;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity3 extends Activity {
Button button;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
Toast.makeText(this, "Activity3", Toast.LENGTH_SHORT).show();
getActionBar().setDisplayHomeAsUpEnabled(true);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
activity3();
}
});
}
public void activity3() {
Intent intent = new Intent(this, com.example.acct.MainActivity3.class);
startActivity(intent);
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.acct"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.acct.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity2"
android:parentActivityName=".MainActivity" >
</activity>
<activity
android:name=".MainActivity3"
android:parentActivityName=".MainActivity2" >
</activity>
</application>
</manifest>
My Main Activity class is as follows
package com.example.barnight2;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView date;
private Button taxiButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
dateAndTime();
}
public void addListenerOnButton() {
taxiButton = (Button) findViewById(R.id.btnTaxi);
taxiButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent taxiIntent = new Intent(MainActivity.this, TaxiActivity.class);
startActivity(taxiIntent);
}
});
}
public void dateAndTime() {
date = (TextView) findViewById(R.id.lblDate);
SimpleDateFormat sdf = new SimpleDateFormat("EEEE");
Date day = new Date();
String dayOfTheWeek = sdf.format(day);
date.setText(dayOfTheWeek);
}
}
then i have my second activity called the Taxi Activity
package com.example.barnight2;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class TaxiActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_taxi);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.taxi, menu);
return true;
}
}
Here is my Manifest.xml i thought i had done everything perfectly but it seems to always crash upon start up of the app
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.barnight2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.barnight2.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.barnight2.TaxiActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
</activity>
</application>
</manifest>
![image]http://i62.tinypic.com/16rma8.png
ClassCastException
you're casting an object that it's not the same type you're trying to cast, so that's why it throw a RunTimeException with ClassCastException.
It should be:
taxiButton = (ImageButton) findViewById(R.id.btnTaxi);
Since you declared it as ImageButton in xml.
Please remove :
<intent-filter>
<action android:name="android.intent.action.MAIN" />
</intent-filter>
from your activity com.example.barnight2.TaxiActivity in the AndroidManifest.xml
I am currently working on a fahrenheit to celsius converter android app. I tried runing the app on the emulator and every time it gives "your app has stopped suddenly" error. tried many things like increasing ram of emulator, rebooting . I also added the activity to manifest.xml.
The mainactivity.java code
package com.example.yoyo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.Menu;
public class MainActivity extends Activity {
EditText input;
EditText answer;
TextView celsius;
TextView fahrenheit;
Button convert;
#Override
protected void onCreate(Bundle savedInstanceState)
{
this.findAllViewsById();
convert.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
String query = input.getText().toString();
answer.setText(query);
}
});
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.main, menu);
return true;
}
public void findAllViewsById()
{
input = (EditText) findViewById(R.id.Input);
answer= (EditText) findViewById(R.id.Answer);
convert= (Button) findViewById(R.id.ConvertButton);
celsius= (TextView) findViewById(R.id.celsius);
fahrenheit= (TextView) findViewById(R.id.fahrenheit);
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yoyo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.yoyo.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.findAllViewsById();
convert.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
String query = input.getText().toString();
answer.setText(query);
}
});
}
first of all the super.onCreate has to been your onCreate does. Second thing you can not do all related findViewById operations if you has not called setContentView
so here is another basic problem i'm having...my application crashes as i try to pass a string to another activity. i have tried a few ways but in vain. here's the code
Main Activity named Main.java
package com.ammad.test;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main extends Activity {
TextView tv;
Button b1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tvShow);
b1 = (Button) findViewById(R.id.bt1);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Bundle basket = new Bundle();
String myScore = "testing";
basket.putString("key", myScore);
Intent intent = new Intent(Main.this, Second.class);
intent.putExtras(basket);
startActivity(intent);
}
});
}
}
Second class to which the data is passes named Second.java
package com.ammad.test;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Second extends Activity{
TextView tv;
String getScore;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
tv= (TextView) findViewById(R.id.tvRes);
Bundle gotBasket = getIntent().getExtras();
getScore = gotBasket.getString("key");
tv.setText(getScore);
}
}
and finally the manifest file:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ammad.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Main"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Sec"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="com.ammad.test.SECS" />
</intent-filter>
</activity>
</application>
</manifest>
Seems to me your second activity is declared wrong in the manifest, as .Sec not .Second as would be expected - the log is probably showing a exception because the intent was invalid.