force down error in app - android

I have a problem on running my app but i cant find out where is the problem! I have created a first screen that has two buttons. This "home" screen is chordsApp.java. I want to go to the track1.java if I press the first button and to track2.java if I press the second! The program runs but when I'm trying to press the first or the second button it pops up an error and tells me to force down!! Here is the code:
ChordApp.java
package com.example.ChordsApp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ChordsApp extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.button);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), track1.class);
startActivityForResult(myIntent,0);
}
});
Button next1 = (Button) findViewById(R.id.button1);
next1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), track2.class);
startActivityForResult(myIntent, 0);
}
});
}
}
track1.java
package com.example.ChordsApp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class track1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Button next = (Button) findViewById(R.id.button3);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
track2.java
package com.example.ChordsApp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class track2 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main3);
Button next1 = (Button) findViewById(R.id.button4);
next1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/pic"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/guitar"
android:layout_gravity="center"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" >
<Button
android:text="track1"
android:id="#+id/button"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px" />
<Button
android:text="track2"
android:id="#+id/button1"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px" />
</LinearLayout>
</LinearLayout>
main2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="hi"/>
<Button android:text="Home"
android:id="#+id/button3"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px"
android:layout_gravity="center_horizontal"
>
</Button>
</LinearLayout>
main3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="hi"/>
<Button android:text="Home"
android:id="#+id/button4"
android:layout_width="250px"
android:textSize="18px"
android:layout_height="55px"
android:layout_gravity="center_horizontal"
>
</Button>
</LinearLayout>

Related

How to debug an Android app that says "unfortunately <app> stopped working"?

This is my java class, MainActivity.java.
Basically I'm trying to create a button performing addition on variable i. Everything seems to be correct as far as I learnt until now. I am getting an error message on my emulator saying:
unfortunately myfirstandroidapp stopped working.
Code:
package com.example.android.myfirstandroidapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
int i = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button button = (Button) findViewById(R.id.addButton);
final TextView txtView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
i++;
txtView.setText("i value is:"+i);
}
});
setContentView(R.layout.activity_main);
}
}
This is my layout.xml
main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.android.myfirstandroidapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your value is:0"
android:id="#+id/textView"
android:textColor="#android:color/black"
android:textStyle="normal|bold"
android:textSize="14dp" />
<Button
android:text="Add One"
android:layout_width="100dp"
android:layout_height="20dp"
android:layout_marginTop="25dp"
android:id="#+id/addButton"
android:background="#android:color/holo_green_dark"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
You need to call setContentView() before initializing any View.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.addButton);
final TextView txtView = (TextView) findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
i++;
txtView.setText("i value is:"+i);
}
});
}

Password protect android app

I have an application which I am developing. I need to password protect it, i.e. a password needs to be entered before allowing access to the main content.
Password can be stored locally in the Java file.
LoginActivity.java
package com.example.capitacustomerfactsheets;
import android.os.Bundle;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class LoginActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_login);
final EditText passWord = (EditText) findViewById(R.id.editpassword);
final Button loginButton = (Button) findViewById(R.id.btn_submit);
loginButton .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(passWord.getText().toString().equals("123456")) {
final Intent myIntent = new Intent();
myIntent.setComponent(new ComponentName(LoginActivity.this,MainActivity.class));
startActivity(myIntent);
finish();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
}
activity_login.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".LoginActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/password" />
<EditText
android:id="#+id/editpassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:ems="10"
android:inputType="textPassword" >
<requestFocus />
</EditText>
<Button
android:id="#+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Login" />
</RelativeLayout>
try this one
public void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.activity_login);
final EditText passWord = (EditText) findViewById(R.id.editpassword);
final Button loginButton = (Button) findViewById(R.id.btn_submit);
loginButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(passWord.getText().toString().equals("123456")) {
final Intent myIntent = new Intent();
myIntent.setComponent(new ComponentName(LoginActivity.this,your new activity.class));
startActivity(myIntent);
finish();
}
}
});
}

Toast String display object instead of Text

In my code,I want to display TextView text name as Toast message.
I do following coding for that .I didn't get the proper text only object of that text I got.
In Toast I want to display the Text please guide me if possible.
Here is my code :
package com.Viewflipper;
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;
import android.widget.ViewFlipper;
public class Viewflipper extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button next;
Button previous;
ViewFlipper vf;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vf = (ViewFlipper) findViewById(R.id.ViewFlipper01);
next = (Button) findViewById(R.id.Button01);
previous = (Button) findViewById(R.id.Button02);
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == next) {
vf.showNext();
Toast.makeText(this,vf.toString(), Toast.LENGTH_SHORT).show();
**//HERE I DIDN'T GET vf.getText().toString();**
}
if (v == previous) {
vf.showPrevious();
}
}
}
**EDITED**
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical">
<LinearLayout android:id="#+id/LinearLayout03"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:id="#+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Next"></Button>
<Button android:id="#+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Previous"></Button>
</LinearLayout>
<LinearLayout android:id="#+id/LinearLayout02"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<ViewFlipper android:id="#+id/ViewFlipper01"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<!--adding views to ViewFlipper-->
<TextView android:id="#+id/TextView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/a10"
android:text="one"></TextView>
<TextView android:id="#+id/TextView02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/a11"
android:text="two"></TextView>
<TextView android:id="#+id/TextView03" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/a12"
android:text="three">
</TextView>
<TextView android:id="#+id/TextView04" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/a13"></TextView>
<TextView android:id="#+id/TextView05" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/a14"></TextView>
<TextView android:id="#+id/TextView06" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:background="#drawable/a15"></TextView>
</ViewFlipper>
</LinearLayout>
</LinearLayout>
Try This:-
package com.Viewflipper;
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;
import android.widget.ViewFlipper;
public class Viewflipper extends Activity implements OnClickListener {
/** Called when the activity is first created. */
Button next;
Button previous;
ViewFlipper vf;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vf = (ViewFlipper) findViewById(R.id.ViewFlipper01);
next = (Button) findViewById(R.id.Button01);
previous = (Button) findViewById(R.id.Button02);
next.setOnClickListener(this);
previous.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == next) {
vf.showNext();
Toast.makeText(getApplicationContext(),((TextView) vf.getCurrentView()).getText(), Toast.LENGTH_SHORT).show();
}
if (v == previous) {
vf.showPrevious();
}
}
}
A ViewFliper is an adapter.. you can't just grab it like that, you have to interact with the adapter and get the view and THEN grab the textview in that view.

I get a forcestop occasionally when I press buttons other than the first

I have 3 buttons in total. And it seemed that, if I don't press the first button, and instead second or third button right after I startup, my application will forcestop.
My code for part1 : http://pastebin.com/udkEdF3E
package project.ernest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Part1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button a = (Button) findViewById(R.id.Disclaimer);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent b = new Intent(getBaseContext(), Part2.class);
Part1.this.startActivity(b);
final Button c = (Button) findViewById(R.id.Orientation);
c.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent d = new Intent(getBaseContext(), Part3.class);
Part1.this.startActivity(d);
final Button e = (Button) findViewById(R.id.Course);
e.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent f = new Intent(getBaseContext(), Part4.class);
Part1.this.startActivity(f);
}
});
};
});
};
});
};
}
my code for main.xml : http://pastebin.com/SxfxfJ4a
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/myColor" android:weightSum="1">
<LinearLayout android:orientation="vertical" android:id="#+id/linearLayout1" android:weightSum="1" android:layout_width="wrap_content" android:layout_height="404dp">
<TextView android:text="#string/HospName" android:id="#+id/textView1" android:textSize="20mm" android:layout_width="324dp" android:layout_height="142dp"></TextView>
<Button android:text="#string/Disclaimer" android:layout_weight="0.37" android:textSize="5mm" android:layout_height="75dp" android:layout_width="fill_parent" android:id="#+id/Disclaimer" android:drawingCacheQuality="high" android:onClick="#string/DisclaimerHandler"></Button>
<Button android:text="#string/Orientation" android:textSize="5mm" android:layout_height="75dp" android:layout_weight="0.37" android:layout_width="fill_parent" android:id="#+id/Orientation" android:onClick="#string/OrientationHandler"></Button>
<Button android:text="#string/Course" android:textSize="5mm" android:layout_height="75dp" android:layout_weight="0.37" android:layout_width="fill_parent" android:id="#+id/Course" android:onClick="#string/CourseHandler"></Button>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_weight="0.19" android:layout_height="wrap_content" android:id="#+id/linearLayout2" android:weightSum="1">
<Button android:layout_weight="0.23" android:text="#string/Search" android:textSize="1.9mm" android:layout_height="75dp" android:layout_width="75dp" android:onClick="#string/SearchHandler" android:id="#+id/Search"></Button>
<Button android:layout_weight="0.23" android:text="#string/Contact" android:textSize="1.9mm" android:layout_height="75dp" android:layout_width="75dp" android:onClick="#string/ContactHandler" android:id="#+id/Contact"></Button>
<Button android:onClick="#string/LinkHandler" android:layout_height="75dp" android:layout_width="75dp" android:layout_weight="0.23" android:textSize="1.9mm" android:text="#string/Link" android:id="#+id/Link"></Button>
<Button android:layout_weight="0.23" android:text="#string/Copyright" android:textSize="1.9mm" android:layout_height="75dp" android:layout_width="75dp" android:onClick="#string/CopyrightHandler" android:id="#+id/Copyright"></Button>
</LinearLayout>
</LinearLayout>
The error I get is either:
java.lang.IllegalStateException: Could not find a method CourseHandler(View) in activity class project.ernest.Part1 or...
java.lang.IllegalStateException: Could not find a method OrientationHandler(View) in activity class project.ernest.Part1 or...
Please help me out!
You've placed initialization of buttons 2 and 3 inside the first button's onClickListener(), means they won't be initialized before you click the first button. Fix your code.
this is the format.
public class Part1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button a = (Button) findViewById(R.id.Disclaimer);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent b = new Intent(getBaseContext(), Part2.class);
Part1.this.startActivity(b);
}
});
final Button c = (Button) findViewById(R.id.Orientation);
c.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent d = new Intent(getBaseContext(), Part3.class);
Part1.this.startActivity(d);
}
});
final Button e = (Button) findViewById(R.id.Course);
e.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent f = new Intent(getBaseContext(), Part4.class);
Part1.this.startActivity(f);
}
});
}
}

layout elements not showing up

I have two activities and the first is generating properly however the second is not. only the first element in the xml file will show up. here is my xml file:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/alternateActivity"
android:shadowColor="#color/shadowColor"></TextView>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/oldActivityButton"
android:text="#string/oldActivityButton"></Button>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/changeText"
android:text="#string/changeTextButton"></Button>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/originalText"
android:id="#+id/textToChange"></TextView>
</LinearLayout>
and here is my activity that corresponds:
package fsg.dev.activitytest;
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 newActivity extends Activity {
private Button oldActivityButton;
private Button changeText;
private TextView textToChange;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alternate);
oldActivityButton = (Button) findViewById(R.id.oldActivityButton);
changeText = (Button) findViewById(R.id.changeText);
textToChange = (TextView) findViewById(R.id.textToChange);
oldActivityButton.setOnClickListener(new OnClickListener(){
public void onClick(View view){
changeActivity();
}
});
changeText.setOnClickListener(new OnClickListener(){
public void onClick(View view){
changeText();
}
});
}
private void changeActivity(){
Intent i = new Intent(this, activityTest.class);
startActivity(i);
}
private void changeText(){
if(textToChange.equals(R.string.originalText)){
textToChange.setText(R.string.newText);
} else {
textToChange.setText(R.string.originalText);
}
}
}
has anyone else seen this problem? or know of a way to fix it?
Try to add android:orientation="vertical" to your LinearLayout
replace
Intent i = new Intent(this, activityTest.class);
by
Intent i = new Intent().setClass(this, activityTest.class);

Categories

Resources