Bugagull
Bugagull

Reputation: 27

Unsuccessful opening second activity

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>

Upvotes: 1

Views: 95

Answers (5)

Sachin Suthar
Sachin Suthar

Reputation: 692

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);
      }
 }

Upvotes: 0

Ariel Salas
Ariel Salas

Reputation: 9

Replace {if (v == button2)for {if (v == button2.getView())or try to find by ID. I recommend use:

button2.setOnclickListener(new OnClickListener {

    // TO DO

})

Upvotes: 0

G&#246;rkem Karayel
G&#246;rkem Karayel

Reputation: 1

overriding the button2 onclick listener. Programmatic this show;

 button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClickShop(v);
            }
        });

Upvotes: 0

Jonathan Monga
Jonathan Monga

Reputation: 1

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.

Upvotes: 0

Jo&#227;o Ara&#250;jo
Jo&#227;o Ara&#250;jo

Reputation: 41

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.

Upvotes: 4

Related Questions