So I am trying to open a second activity from the first activity from a click of a button. The app loads up but then when I try to press the button to go to the next activity nothing happens. Here is my main.xml, mainactivity.java, androidmanifest.xml, shopactivity.java and shop.xml, respectively. Any help is greatly appreciated.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="#string/counter"
android:textColor="#000000"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:text="#string/gems"
android:textColor="#000000"/>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textView1"
android:layout_alignBaseline="#+id/textView1"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="#000000"/>
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textView2"
android:layout_alignBaseline="#+id/textView2"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:textColor="#000000"/>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="310dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#000000"
android:text="#string/button"
android:textColor="#ffffff"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:background="#ffffff"
android:text="#string/button2"
android:textColor="#000000"
android:onClick="onClickShop"/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_alignParentRight="true"
android:background="#ffffff"
android:text="#string/button3"
android:textColor="#000000"/>
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_toRightOf="#+id/button2"
android:layout_toLeftOf="#+id/button3"
android:background="#ffffff"
android:text="#string/button4"
android:textColor="#000000"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity implements OnClickListener{
TextView textView1;
TextView textView2;
EditText editText1;
EditText editText2;
Button button1;
Button button2;
Button button3;
Button button4;
int counter = 0;
int gems = 0;
#Override
public void onCreate(Bundle savedInstanceState)
{super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.main);
textView1 = (TextView)findViewById(R.id.textView1);
textView2 = (TextView)findViewById(R.id.textView2);
editText1 = (EditText)findViewById(R.id.editText1);
editText2 = (EditText)findViewById(R.id.editText2);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
button4 = (Button)findViewById(R.id.button4);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);}
#Override
protected void onStop()
{super.onStop();
SharedPreferences prefs = this.getSharedPreferences("com.bugagullgames.clicker", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit(); editor.putInt("key", counter); editor.commit();}
#Override
protected void onStart()
{super.onStart(); SharedPreferences prefs = this.getSharedPreferences("com.bugagullgames.clicker", Context.MODE_PRIVATE);
int defaultValue = 0;
counter = prefs.getInt("key", defaultValue);
editText1.setText(Integer.toString(counter));}
#Override
public void onClick(View v)
{if (v == button1)
{counter++; editText1.setText(Integer.toString(counter));}}
public void onClickShop(View v)
{if (v == button2)
{
Intent intent = new Intent(MainActivity.this, ShopActivity.class);
startActivity(intent);
}}}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bugagullgames.clicker" >
<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=".ShopActivity"
android:label="#string/app_name">
</activity>
</application>
</manifest>
public class ShopActivity extends MainActivity
{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shop);
}}
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:text="Welcome to the shop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
You are overriding the button2 onclick listener. Remove this line of code:
button2 = (Button)findViewById(R.id.button2);
And the xml onclick property that you configured android:onClick="onClickShop" should work.
hello #Bugagull define your "ShopActivity.class" in your menifest.
<activity
android:name=".ShopActivity"
android:label="#string/app_name">
</activity>
and replace this .
public class ShopActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.shop);
}
}
If you want use a xml method .
your declaration method must be like it.
public void methodname(View v)
it's important to put this argument.
overriding the button2 onclick listener. Programmatic this show;
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClickShop(v);
}
});
Replace {if (v == button2)for {if (v == button2.getView())or try to find by ID.
I recommend use:
button2.setOnclickListener(new OnClickListener {
// TO DO
})
Related
I am very new at android development, and I am trying to make an app which has 4 buttons in its main activity and when I click on one of its button it takes me to another activity and displays its xml file, what should I write in the 2nd activity? Here is my code so far.
main xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:background="#color/colorAccent"
android:text="Overview"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Information" />
<TableRow
android:id="#+id/hr1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Education" />
<TableRow
android:id="#+id/hr2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Work Experience" />
<TableRow
android:id="#+id/hr3"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
<Button
android:id="#+id/four"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:text="Education" />
<TableRow
android:id="#+id/hr4"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#444">
</TableRow>
</LinearLayout>
</LinearLayout>
........................
main activity
package com.lakshay.display.piechart;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements
View.OnClickListener {
Button btn1 , btn2 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Intent intent = new Intent();
String nextAct = null ;
String shield = "com.lakshay.display.piechart";
Integer flag= -1;
switch (v.getId())
{
case (R.id.one ):
nextAct = shield + "ContactActicity";
break;
default:
Toast.makeText(MainActivity.this , "Item Currently Unavailable"
, Toast.LENGTH_SHORT).show();
}
try {
if (nextAct!=null)
{
intent = new Intent(MainActivity.this , Class.forName(nextAct));
flag = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT;
if (flag != -1 ){
intent.setFlags(flag);
} startActivity(intent);
}
} catch (ClassNotFoundException e){
e.printStackTrace();
}
}
}
...................
xml file for 2nd activity
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorAccent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
tools:context="com.lakshay.display.piechart.ContactActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Name"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/background_light"
android:inputType="text"
android:paddingBottom="20dp"
android:paddingLeft="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Your Address"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPostalAddress"
android:id="#+id/address"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:background="#android:color/background_light"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Phone Number"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:id="#+id/number"
android:paddingBottom="20dp"
android:background="#android:color/background_light"
android:paddingLeft="20dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email"
android:textStyle="bold"
android:paddingLeft="20dp"
android:textSize="22dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:id="#+id/email"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:background="#android:color/background_light"
/>
</LinearLayout>
...................
My answer as a checklist:
1.- If you are using android studio you should create 2nd activity with the assistant so you dont get into more complications.
The 2nd activity must have an xml file and a class file.
2.- You should add android:onClick propertie for your button in the xml file on the activity.
3.- Your 2nd activity must have an onCreate method in the class file to fill the contents of the 2nd activity.
3b.- You can leave onCreate with default content but your xml file must then have al the info of your textviews.
I can elaborate further if needed.
Let's say you have two activities : Activity1.java and Activity2.java.
to start Activity2 from Activity1 just do :
final Intent intent = new Intent(this, Activity2.class);>startActivity(intent)
If you want to start activity on button click you have to write this codein an onClickListener. To do that, write the following attribute in your button definition in Activity1 xml file.
android:onCLick="onButtonClick"
Then in your activity write the following listener :
public void onButtonClick(final View v) {
// put your intent here
}
This code help you ....
public class MainActivity extends AppCompatActivity {
Button btn1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.btn1);
final String name = editText.getText().toString();
button.setOnClickListener(new View.OnClickListener() {//when your btn1 button press
public void onClick(View v) {
Intent intent = new Intent(this, Activity2.class);//this is call your second activity
startActivity(intent);//start activity
}
});
}
}
First of all, you must get your Button id from your xml. Or you will get NullPointerException so change your onCreate like this.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.one); //This line
btn2 = (Button)findViewById(R.id.two); //and this line
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
And if you want to call intent with Class, you can see this solution
OR
You can simply call another activity like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.one); //This line
btn2 = (Button)findViewById(R.id.two); //and this line
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(firstActivity.this, secondActivity.class));
}
});
}
I'm new in android and I just write a test app that should take the texts of the two buttons & change the text of them to each other in the new activity with intent.
but when I run the app and click the first button, it goes to second activity and in the second activity it just showing only the text of that button I just clicked on for both 2 buttons! actually, it should change the text of buttons to each other.(I mean the text of button 1 should display in button 2 and vice versa)
I check every question about the intent here, but my case is waired!!! because it seems everything is right! also, I check the code many times, I don't realize what's wrong!!!!
Thanks.
Here is the code:
MainActivity
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button buttonone,buttontwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void changeText(View view){
buttonone=(Button) view;
buttontwo=(Button) view;
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("button1text",buttonone.getText().toString());
intent.putExtra("button2text",buttontwo.getText().toString());
startActivity(intent);
}
}
activity_main.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.myb.assignment.MainActivity">
<Button
android:text="One"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="35dp"
android:layout_marginTop="46dp"
android:id="#+id/buttonone"
android:onClick="changeText"/>
<Button
android:text="Two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="59dp"
android:id="#+id/buttontwo"
android:onClick="changeText"
android:layout_alignBaseline="#+id/buttonone"
android:layout_alignBottom="#+id/buttonone"
android:layout_alignParentEnd="true" />
</RelativeLayout>
SecondActivity
public class SecondActivity extends Activity {
Button button1,button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
#Override
public void onResume(){
super.onResume();
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
String intentData1=getIntent().getExtras().getString("button1text");
String intentData2=getIntent().getExtras().getString("button2text");
button1.setText(intentData2);
button2.setText(intentData1);
}
}
activity_second.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button1"
android:layout_alignParentEnd="true"
android:layout_marginEnd="59dp"
android:id="#+id/button2"
/>
<Button
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="34dp"
android:layout_marginTop="46dp"
android:id="#+id/button1"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myb.assignment">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"> </activity>
</application>
</manifest>
In your MainActivity, in changeText(),
buttonone=(Button) view;
buttontwo=(Button) view;
These two lines assign the same button view, to both the buttons.
Insted of this, I recommend doing the following in your onCreate() method after setContentView();
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
And remove those two lines in changeText().
I have three XML files are
1) Header
2) FirstActivity
3) SecondActivity.
header.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Button 1"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="first1"
>
</Button>
<Button
android:text="Button 2"
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="second"
>
</Button>
</LinearLayout>
firstactivity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<include layout="#layout/headerfile"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="100dip"
>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone Detail"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/simid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/imeino"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/phoneno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
</RelativeLayout>
HeaderActivity.java
public class HeaderActivity extends Activity {
Button b1,b2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.headerfile);
b1 = (Button)findViewById(R.id.button1) ;
b2 = (Button)findViewById(R.id.button2) ;
} //onCreate over
public void first1(View v)
{
Intent i =new Intent(this,firstactivity.class);
startActivity(i);
finish();
}
public void second(View v)
{
Intent i1 =new Intent(this, secondactivity.class);
startActivity(i1);
finish();
}
}
But I got ERROR every time …
Log-cat :
ERROR/AndroidRuntime(328): java.lang.IllegalStateException: Could not find a method first1
(View) in the activity class com.contactDetails.ContactDetailsActivity for onClick handler on view class
android.widget.Button with id 'button1'
In ContactDetailsActivity.java class you should add below method
public void first1(View v)
{
// onlick code
}
<include> tags include all the elements in your root layout. So you can always access them using findViewById on your Activity.
You could use another way to handle onClickListener of your Button..
You don't have to write separate Activity for handling include layout views....
you can directly access that buttons from activity where you have used view (i.e. firstactivity.xml).
Try This..
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i =new Intent(this,firstactivity.class);
startActivity(i);
finish();
}
});
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i1 =new Intent(this,secondactivity.class);
startActivity(i1);
finish();
}
});
and Remove onClick property from both Button
Hi please lead me some examples which demonstrate how to create editText with "delete" button , when click on the delete option it will remove the created editText . Thanks in advance ...
code for creating textEdit dynamically..
LinearLayout rAlign = (LinearLayout)findViewById(R.id.lId);
EditText newPass = new EditText(getApplicationContext());
allEds.add(newPass);
newPass.setHint("Name of Label");
newPass.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
newPass.setWidth(318);
newPass.setTextColor(Color.parseColor("#333333"));
newPass.setId(MY_BUTTON);
//newPass.
//newPass.setOnClickListener(this);
rAlign.addView(newPass);
MY_BUTTON ++;
addSpinner(); //Function for adding spinner
The layout could be something like this:
<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" >
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="50dp"
android:ems="10" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/editText1"
android:layout_alignBottom="#+id/editText1"
android:layout_toRightOf="#+id/editText1"
android:text="Button" />
And the Java could be like this:
public class MainActivity extends Activity implements OnClickListener {
TextView tv;
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button1);
tv = (TextView) findViewById(R.id.editText1);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
tv.setVisibility(View.GONE);
}
}
I need your help. Can you help me resolving this project without encountering a "FATAL ERROR: Main" or something? I want to make a program that has the following steps:
Register or sign-up a name for player one and two into the spinner. The EditText turn empty afterwards.
Name(s) appeared on their respective spinners. (Duplication of names is OK for me for player one and player two)
Whatever the player's name selected on the spinner and if I click "Set your name(s) and play!" button, the intent calls for another .class for the result.
The only thing I didn't expected is the annoying FORCE CLOSE error even if I got no errors. Here's my sample:
Here's my code for the main class (AndroidSpinnerFromSQLiteActivity):
//Variables
private Spinner spinner, spinner_2;
private Button add_button, add_button_2;
private EditText label_input, label_input_2;
//Response
public final static String EXTRA_MESSAGE_ONE = "com.example.databasetest.MESSAGEONE";
public final static String EXTRA_MESSAGE_TWO = "com.example.databasetest.MESSAGETWO";
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Identifying view by looking for the player 1 view's ID.
spinner = (Spinner) findViewById(R.id.Player_1_Spinner);
add_button = (Button) findViewById(R.id.Player_1_Sign_up_Button);
label_input = (EditText) findViewById(R.id.Player_1_Text_Field);
//Identifying view by looking for the player 2 view's ID.
spinner_2 = (Spinner) findViewById(R.id.Player_2_Spinner);
add_button_2 = (Button) findViewById(R.id.Player_2_Sign_up_Button);
label_input_2 = (EditText) findViewById(R.id.Player_2_Text_Field);
//Adding the spinner listener...
spinner.setOnItemSelectedListener(this);
spinner_2.setOnItemSelectedListener(this);
//Loading spinner's data from the database.
loadSpinnerData();
//Function for Buttons (Player 1)
add_button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
String label = label_input.getText().toString();
//Here's the process on how to register in the database.
if(label.trim().length() > 0)
{
//Database Handler from Class (Database_Handler.java)
Database_Handler db = new Database_Handler(getApplicationContext());
//Inserting new label into the database.
db.insertLabel(label);
//After typing, the text field is set to blank.
label_input.setText("");
//Normally, most smartphones and tablets only have a virtual keyboard.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(label_input.getWindowToken(), 0);
loadSpinnerData();
}
else //If the input is null...
{
Toast.makeText(getApplicationContext(), "Please enter your name, player 1!", Toast.LENGTH_SHORT).show();
}
}
});
//Function for Buttons (Player 2)
add_button_2.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
String label = label_input_2.getText().toString();
//Here's the process on how to register in the database.
if(label.trim().length() > 0)
{
//Database Handler from Class (Database_Handler.java)
Database_Handler db = new Database_Handler(getApplicationContext());
//Inserting new label into the database.
db.insertLabel(label);
//After typing, the text field is set to blank.
label_input_2.setText("");
//Normally, most smartphones and tablets only have a virtual keyboard.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(label_input_2.getWindowToken(), 0);
loadSpinnerData();
}
else //If the input is null...
{
Toast.makeText(getApplicationContext(), "Please enter your name, player 2!", Toast.LENGTH_SHORT).show();
}
}
});
}
public void sendMessage(View v)
{
Intent intent = new Intent(this, Respond_Test.class);
EditText P1 = (EditText) findViewById(R.id.Player_1_Text_Field);
String message1 = P1.getText().toString();
EditText P2 = (EditText) findViewById(R.id.Player_2_Text_Field);
String message2 = P2.getText().toString();
intent.putExtra(EXTRA_MESSAGE_ONE, message1);
intent.putExtra(EXTRA_MESSAGE_TWO, message2);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//Now, call a method called loadSpinnerData() from the onCreate() method.
private void loadSpinnerData()
{
Database_Handler db = new Database_Handler(getApplicationContext());
List<String> lables = db.getAllLabels();
//Creating an adapter for the spinner...
ArrayAdapter<String> data_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lables);
data_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(data_adapter);
spinner_2.setAdapter(data_adapter);
}
//Action applied if a user chose this item.
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
String label = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "You selected: " + label,
Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0)
{
//Do nothing. I guess...
}
Here's another code for the response of displaying names on another class (Respond_Test):
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String messageone = intent.getStringExtra(AndroidSpinnerFromSQLiteActivity.EXTRA_MESSAGE_ONE);
String messagetwo = intent.getStringExtra(AndroidSpinnerFromSQLiteActivity.EXTRA_MESSAGE_TWO);
TextView P1 = (TextView) findViewById(R.id.Player_1_ID);
TextView P2 = (TextView) findViewById(R.id.Player_2_ID);
P1.setText(messageone);
P2.setText(messagetwo);
setContentView(R.layout.respond);
}
The XML of the main (main.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" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="41dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Player 1 Name:" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout1" >
<EditText
android:id="#+id/Player_1_Text_Field"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:inputType="textNoSuggestions"
android:textSize="#dimen/padding_large" >
<requestFocus />
</EditText>
<Button
android:id="#+id/Player_1_Sign_up_Button"
android:layout_width="70dp"
android:layout_height="35dp"
android:text="#string/Sign_Up"
android:textSize="#dimen/padding_medium" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout2" >
<Spinner
android:id="#+id/Player_1_Spinner"
android:layout_width="wrap_content"
android:layout_height="37dp"
android:layout_weight="1"
android:prompt="#string/PLAYER_1_PROMPT"
tools:listitem="#android:layout/simple_spinner_dropdown_item" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout3" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="41dp"
android:text="Enter Player 2 Name:" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout4" >
<EditText
android:id="#+id/Player_2_Text_Field"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="5.47"
android:inputType="textPersonName" />
<Button
android:id="#+id/Player_2_Sign_up_Button"
android:layout_width="70dp"
android:layout_height="35dp"
android:text="#string/Sign_Up"
android:textSize="#dimen/padding_medium" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/linearLayout5" >
<Spinner
android:id="#+id/Player_2_Spinner"
android:layout_width="wrap_content"
android:layout_height="37dp"
android:layout_weight="1"
android:prompt="#string/PLAYER_2_PROMPT" />
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="34dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="62dp"
android:text="#string/GAME"
android:onClick="sendMessage" />
</RelativeLayout>
XML for the response (**respond.xml**):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/Player_1_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="64dp"
android:text="TextView"
android:textSize="20sp" />
<TextView
android:id="#+id/Player_2_ID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Player_1_ID"
android:layout_below="#+id/Player_1_ID"
android:layout_marginTop="24dp"
android:text="TextView"
android:textSize="20sp" />
</RelativeLayout>
And the manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.databasetest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".AndroidSpinnerFromSQLiteActivity"
android:label="#string/title_activity_android_spinner_from_sqlite"
android:theme="#style/AppTheme">"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Respond_Test"
android:label="#string/title_activity_android_spinner_from_sqlite"
android:theme="#style/AppTheme"/>"
</application>
</manifest>
Hope you can help me and thanks in advance.
Try this Activity one
Intent intent = new Intent(yourActivity.this, Respond_Test.class);
intent.putExtra("EXTRA_MESSAGE_ONE", message1);
intent.putExtra("EXTRA_MESSAGE_TWO", message2);
startActivity(intent);
secondActivity
messageone = getIntent().getExtras().getString("EXTRA_MESSAGE_ONE");
messagetwo = getIntent().getExtras().getString("EXTRA_MESSAGE_TWO");
P1.setText(messageone );
P2.setText(messagetwo );