Reputation: 343
I have been trying to switch view using Intent and onClick. When i click on the button my app crashes. I have browsed trough similiar problems asked here but none of them seem to apply to what i have. Anyway, I was wondering if anyone could give me a vague hint on what is wrong with my code.
The button XML:
<Button
android:id="@+id/shapes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SHAPES"
android:textSize="16dp"
android:paddingBottom="20dp"
android:paddingTop="20dp"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:layout_marginTop="30dp"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:onClick="onShapes"/>
When button is clicked start Shapes.class (from MainActivity):
public void onShapes(View view) {
Intent startShapes = new Intent(getApplicationContext(), Shapes.class);
startActivity(startShapes);
}
Shapes.class, sets view to content_shapes:
package com.example.******.****.buttonns;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import com.example.******.****.R;
public class Shapes extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_shapes);
}
}
content_shapes (the view i want to see when pressing the button):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.*******.*****.buttonns.Shapes">
<TextView
android:text="Helloooo"
android:gravity="center"
android:textSize="32dp"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Shapes_text" />
</RelativeLayout>
And i also put this in the Manifest:
<activity
android:name=".buttonns.Shapes"
android:label="@string/title_activity_shapes"
android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.********.*****.MainActivity" />
</activity>
I have been trying to figure out the fault for hours now. By the way I am a total noob at Java, XML and Android Studio so dont hate me if I forgot something trivial.
Upvotes: 0
Views: 2836
Reputation: 271
use this :
Intent startShapes = new Intent(MainActivity.this, Shapes.class);
startActivity(startShapes);
instead of this :
Intent startShapes = new Intent(getApplicationContext(), Shapes.class);
startActivity(startShapes);
Upvotes: 4