Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialHabiballah Hezarehee
Android Development Techdegree Student 6,312 Pointsproblem with button view and java codes in android app
I can run my app successfully but unfortunately I get an error while I am pressing the button which get me confused. I did the project according to a tutorial and can run the tutorials code successfully but not mine. Well can someone help me to find a solution? :)
Here is the XML code:
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/wallpaper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="340dp"
android:paddingTop="0dp"
android:src="@drawable/wallpaper" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="270dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="15dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:text="Barcelona"
android:textColor="#616161"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Goal(s):"
android:textSize="10sp" />
<TextView
android:id="@+id/goalBarcelona"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Foul(s):" />
<TextView
android:id="@+id/foulBarcelona"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="oneGoalToBarcelona"
android:text="Goal: " />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="oneFoulToBarcelona"
android:text="Foul: " />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="15dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:text="Real Madrid"
android:textColor="#616161"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Goal(s):"
android:textSize="20sp" />
<TextView
android:id="@+id/goalRealMadrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="increaseGoalByOneRealMadrid"
android:text="0" />
<TextView
android:id="@+id/foul"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Foul(s):" />
<TextView
android:id="@+id/foulRealMadrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="oneGoalToRealMadrid"
android:text="0" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="addGoalRealMadrid"
android:text="Goal" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="addFoulRealMadrid"
android:text="Foul" />
</LinearLayout>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:gravity="center"
android:onClick="gameOver"
android:text="Game over" />
</RelativeLayout>
and here I want to show a number when someone click goal for real madrid:
package com.hezarehee.android.courtcenter;
import android.view.View; import android.widget.Button; import android.widget.TextView; import android.support.v7.app.AppCompatActivity; import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
int plusOneGoalRealMadrid = 0; // this integer variable will save the number of goal
/**
* this method will add one goal for real madrid
*/
public void increaseGoalByOneRealMadrid(View v) {
plusOneGoalRealMadrid = plusOneGoalRealMadrid + 1;
displayGoalRealMadrid(plusOneGoalRealMadrid);
}
/**
* this method will show the goal real madrid1
*/
public void displayGoalRealMadrid(int goal) {
TextView showGoalRM = (TextView) findViewById(R.id.goalRealMadrid);
showGoalRM.setText(String.valueOf(goal));
}
}
Batsirai Gurure
6,553 PointsBatsirai Gurure
6,553 PointsI see that you defined your 2 methods for increaseGoalByOneRealMadrid and displayGoalRealMadrid but I don't see anywhere where you are actually calling either one of these 2 methods. What error message are you getting?