0

I am working on an app which will do the following:

  1. Allow the first user "Sam" to type his/her message in the editText field.
  2. Press the "Alex" button at the bottom of the screen to start the new activity ActivityAlex
  3. Allow user "Alex" to edit the editText field.
  4. Press the "Sam" button at the bottom of the screen to restart the activity MainActivity

So far, I think I have most of the work done but I'm getting stuck on how to fix the program so it will do what I want it to do.

Here is my logcat

Here is my MainActivity:

package com.chatfriends;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    private Button alexButton; //initialize the buttons for "alex" and "sam"

    public void initialize(){

        alexButton = (Button) findViewById(R.id.button1); // initializing the buttons by location. Also need to define the button with two states (regular and yellow)

       alexButton.setOnClickListener(new OnClickListener(){
        public void onClick(View arg0){
        alexbutton(arg0);
        }

        public void alexbutton(View v) {
            Intent intent = new Intent(MainActivity.this, ActivityAlex.class); //the intent is telling the app what you want it to do. Think about setting the intent to the buttons.
            startActivity(intent);

        }
    });
  }
}

Here is my ActivityAlex:

package com.chatfriends;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class ActivityAlex extends MainActivity {

      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


        private Button samButton; //initialize the buttons for "alex" and "sam"

        public void initialize(){

            samButton = (Button) findViewById(R.id.button2); // initializing the buttons by location. Also need to define the button with two states (regular and yellow)

        samButton.setOnClickListener(new OnClickListener(){
            public void onClick(View arg0){
            samButton(arg0);
            }

            public void samButton(View v) {
                Intent intent = new Intent(ActivityAlex.this, MainActivity.class); //the intent is telling the app what you want it to do. Think about setting the intent to the buttons.
                startActivity(intent);

            }
        });
    }
}

Here is my Manifest code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.chatfriends"
    android:versionCode="1"
    android:versionName="1.0" xmlns:tools="http://schemas.android.com/tools" tools:ignore="OldTargetApi">

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" tools:ignore="MissingPrefix"/>

    <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=".ActivityAlex" />
    </application>

</manifest>

Here are the XML layout files.

activity_main:

<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"
    tools:context=".MainActivity" >

        <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/userName1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:ems="10"
        android:inputType="textMultiLine" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="26dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="@string/button1"
        android:onClick="startalexbutton" />

</RelativeLayout>

activityalex.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ActivityAlex" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="@string/userName2"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textView1"
        android:ems="10"
        android:inputType="textMultiLine" >
    </EditText>


    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="26dp"
        android:layout_marginRight="20dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="@string/button2"
        android:onClick="startMainActivity" />

 </RelativeLayout>
Pang
  • 9,564
  • 146
  • 81
  • 122
booky99
  • 1,436
  • 4
  • 27
  • 45

3 Answers3

0
android:onClick="startalexbutton"

This tells your program to call a method startalexbutton() whenever it is pressed, and this method must take a View parameter (public void startalexbutton(View view)).

This method would go in your MainActivity class; however, since you already use setOnClickListener() for it, just remove android:onClick="startalexbutton" from your XML. (You can remove android:onClick="startMainActivity" as well.)

PS: For the future, please put your LogCat logs in text format; then they A) show up in search results and B) are easier to read.

Cat
  • 66,919
  • 24
  • 133
  • 141
0

you are not calling initialize() in onCreate of ActivityAlex and MainActivity Activity to initialize Button

 @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

          initialize() ;//<<<<initialize here
    }

and remove onClick Attribute from both button in xml as

In activity_main.xml :

 <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="26dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="@string/button1"
       />

In activityalex.xml :

 <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="26dp"
        android:layout_marginRight="20dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="@string/button2"
         />

NOTE :

when you are adding setOnClickListener for any View in java code then not need to add android:onClick with View xml

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
0

It's because you registered the button clicks with the following lines for each button in xml.

 <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="26dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="@string/button1"
        android:onClick="startalexbutton" />

 <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="26dp"
        android:layout_marginRight="20dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="@string/button2"
        android:onClick="startMainActivity" />

But you haven't defined any onClick() methods with this names. Either remove these onclick lines from xml or define these methods with the same names for listening to the buttons instead of setting again onClickListeners(). If you decide to define onClick methods with the names that you registered in xml, do the following.

public void startalexbutton(View view)
{
// Do what ever you need here when button is clicked. Because the control comes to this method when the button is clicked. 
}

If you do this, you don't need the initialize() method anymore.

Kanth
  • 6,681
  • 3
  • 29
  • 41