Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android Development for Beginners : Court Counter - DisplayTeamA
/**
* Displays the given score for Team A.
*/
public void displayForTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));
}
@otkrivashkin

This comment has been minimized.

Copy link

commented May 31, 2016

Дякую)

@vsukhomlinov

This comment has been minimized.

Copy link

commented Jul 12, 2016

Щиро дякую

@OleksSobol

This comment has been minimized.

Copy link

commented Jul 16, 2016

=)) українці))) дякую"!

@TheEndIsNear

This comment has been minimized.

Copy link

commented Jul 20, 2016

Shouldn't this method be a private method?

@tahseenbokhari

This comment has been minimized.

Copy link

commented Jul 24, 2016

View tag is not working in my xml code. If I apply that between the two LinearLayouts, it disappears everything from the screen. Can anyone help me in resolving that issue?

@Mahesh78

This comment has been minimized.

Copy link

commented Aug 9, 2016

@tahseenbokhari Can you share your code? You probably made a mistake in specifying appropriate layout height and width.

@eyoryur

This comment has been minimized.

Copy link

commented Sep 15, 2016

tahseenbokhari - You should paste this inside your Java code, not xml.

@SumatiSingh

This comment has been minimized.

Copy link

commented Sep 16, 2016

Super confused. Where can I find the rest of the Java code that I would put this gist into?

@rajabhishek

This comment has been minimized.

Copy link

commented Sep 25, 2016

i cannot see anything happening while calling the display method in on create method.it does not change it remains zero.

@aparnasridhar

This comment has been minimized.

Copy link

commented Sep 28, 2016

Refer this for the entire gist of code if you're confused where to paste it.

@wisnukurniawan

This comment has been minimized.

Copy link

commented Oct 6, 2016

Hye fighting!! haha

@korchix

This comment has been minimized.

Copy link

commented Jan 9, 2017

@TheEndIsNear :
no, otherwise it will not be detected from the xml file ->that mean this methodwill not be used

@formatCEnter

This comment has been minimized.

Copy link

commented Jan 20, 2017

My auto-generated MainActivity.java looked very different from what the instructor had.
Given the circumstance, I expect that any student approach (that actually works) to this isolated portion of the coursework will have to be counted as acceptable. The main point appears to be that:
in activity_main.xml,
under <Button android:id="@+id/teamAPlusThree" (my name for this id)
you put android:onClick="teamAAddThree" (my name for the method)
so that when the teamAPlusThree Button is clicked it wakes up MainActivity.java and tells it to run the teamAAddThree method.
At this point, teamAAddThree does a calculation, scoreA = scoreA + 3;
(remember that you have to declare int scoreA; as a global variable, right under public class MainActivity)
and, after calculating, teamAAddThree calls another method named displayForTeamA,
which changes the number on the screen.
I believe I skipped an intermediary step, but again, all bets are off on this one because of the disparity between the instructors' and students' starting places. Overall, it's not a big problem as long as you get the basic idea of what is happening.
Zooming way out to the bigger picture (and if you dare take the advice of a novice), be careful when you consider upgrading your IDE while you're in the middle of something. It shouldn't be just a knee-jerk reaction to a pop-up on your screen. Even though it doesn't happen that often, stop and consider if you want to risk having to spend 150% more time fixing a project that was once 90% done.

@Liza-S

This comment has been minimized.

Copy link

commented Jan 22, 2017

Thank.

@k-buddy

This comment has been minimized.

Copy link

commented Jan 24, 2017

Error:(21, 36) error: cannot find symbol variable menu
Error:(33, 23) error: cannot find symbol variable action_settings

How to resolve them?

@ghost

This comment has been minimized.

Copy link

commented Jan 28, 2017

getting this error
Error:(1938, 39) error: expected
public static final int team-a-score=0x7f0b0057;

In R.java file

Can someone please help me in this?

@ghost

This comment has been minimized.

Copy link

commented Jan 28, 2017

Guys, the above error was from my end.
I used (-) hyphen instead of ( _ ) dash while assigning ID.

@LVCC01

This comment has been minimized.

Copy link

commented Jan 31, 2017

I had the same issue as described by @formatCEnter - by using the debugging techniques taught in previous lessons I came to a conclusion that the pointer wasn't getting directed to the MainActivity.java methods , even after pressing the button for '+3 POINTS' and implementing android:onClick in XML file. This debugging helped me understand that there could be an issue with the 'touch screen functionality' of the phone rather than the code. Just in case you guys face a similar issue !! By the way, restarting the phone helped me and code worked fine.

@youaresoneighborly

This comment has been minimized.

Copy link

commented Feb 11, 2017

I'm getting error Illegal start of expression??? Also my Java looks different from the instructor Lyla's Java. Any thoughts?

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayForTeamA(8);
/**
* Displays the given score for Team A.
*/
public void displayForTeamA(int score) {
TextView scoreView = (TextView) findViewById(R.id.team_a_score);
scoreView.setText(String.valueOf(score));

@youaresoneighborly

This comment has been minimized.

Copy link

commented Feb 11, 2017

I'm so frustrated by this..Lyla's code is different and I don't feel I can go on with the lesson until I have the right code. I'm surprised this has not been fixed by Udacity.

@Kalinakov

This comment has been minimized.

Copy link

commented Feb 13, 2017

Hi youaresoneighborly,

You forgot to close the braces }

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayForTeamA(8);
}
//before the next comment

/**

  • Displays the given score for Team A.
    */
@Kalinakov

This comment has been minimized.

Copy link

commented Feb 13, 2017

Don´t be frustated any more.
Keep up the good work @youaresoneighborly

@MilenDinev

This comment has been minimized.

Copy link

commented Feb 17, 2017

Its working fine.

@jcunanan05

This comment has been minimized.

Copy link

commented Mar 4, 2017

Thnaks

@joetke

This comment has been minimized.

Copy link

commented Mar 21, 2017

Hi @youaresoneighborly
you're right. Lyla's MainActivity.java and mine (like yours) are different. That shouldn't hurt the exercise but it gives the feeling of a lack of consistency. Udacity should have fixed it since... A bit disappointing
By the way, I develop under ubuntu 16.04. What's your OS? Windows?

@kostromina

This comment has been minimized.

Copy link

commented Mar 22, 2017

It's working fine

@AndiV2

This comment has been minimized.

Copy link

commented Apr 2, 2017

well after an studio update nothing works anymore -.-

@galbalazs

This comment has been minimized.

Copy link

commented Apr 5, 2017

@AndiV2
Uninstall Android Studio and every files, directories which related to Android Studio. And after this install again the program and it must be work. I had the same problem yesterday and this method help me.
I hope it will works for you too!
By the way it means that you delete your previous project in Android Studio, so you want that save them!

@rebekaszucs

This comment has been minimized.

Copy link

commented Apr 5, 2017

working well, thanks!:)

@nnannos

This comment has been minimized.

Copy link

commented Apr 9, 2017

Working! Thanks!

@Vercsi

This comment has been minimized.

Copy link

commented Apr 9, 2017

Thank You! :) (Köszönöm - in Hungarian :) )

@Orest02

This comment has been minimized.

Copy link

commented Apr 10, 2017

Оце так несподіванка в коментах на початку))

@neliamx4

This comment has been minimized.

Copy link

commented Apr 10, 2017

Thanks! :)

@AdeVic

This comment has been minimized.

Copy link

commented Apr 12, 2017

screenshot 2017-04-12 20 31 02
please help a brother.
got stuck here

@TomazMartins

This comment has been minimized.

Copy link

commented Apr 30, 2017

@AdeVic, I think you used a wrong template activity. Are you sure you used an Empty Activity template? If, yes, then I would do another question to you: Are you sure that you used the most actual version of Android Studio? Because, in old version of Android Studio the Empty Activity was different of the current version.

And, if yes, well, you doesn't need this methods, so, you can delete it. What you really need is only the method onCreate() for this lesson.

Another possibility: Does you copied the code of the instructor, in video?
If, yes, Here is the error.

I saw that you use the version 2.3.1 of Android Studio and, in lessons, the instructors use an old version. They explain this in early videos. So, I suggest:

  1. Delete the current project;
  2. Create another one;
  3. Use only this steps -> (a) Put the name, (b) minimum SDK 15, (c) Empty Activity, (d) Default configuration and (e) Put the displayForTeamA( ) below the onCreate( ) method. Don't copy the other methods of the video.

Your MainActivity should be like this, before the initiate the quiz:

public class MainActivity extends AppCompatActivity {

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

    public void displayForTeamA( int score ) {
        TextView scoreView = (TextView) findViewById( R.id.points_teamA );
        scoreView.setText( String.valueOf( score ) );
    }
}
@slickr1ck

This comment has been minimized.

Copy link

commented May 2, 2017

This is unbelievable These codes do not add up and its annoying.

package com.example.android.courtcounter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;

import com.example.android.courtcounter.R;

public class MainActivity extends AppCompatActivity {

private static final android.R.attr R = ;

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

public void displayForTeamA( int score ) {
    TextView scoreView = (TextView) findViewById( R.id.team_a_score);
    scoreView.setText( String.valueOf( score ) );
}

}


image

@slickr1ck

This comment has been minimized.

Copy link

commented May 2, 2017

can some one assist?

@ghost

This comment has been minimized.

Copy link

commented May 3, 2017

2 slickr1ck in settings auto imporn is on?
and delete this line "import com.example.android.courtcounter.R"
also try in red places to type the code manually with an auto-tip

@slickr1ck

This comment has been minimized.

Copy link

commented May 9, 2017

@sazhnevdi i tried that and im still stuck. Now i tried some one else code and it place me at a or the same place i was at

image

@ec12374

This comment has been minimized.

Copy link

commented May 14, 2017

I am using Android Studio 2.3.1, so by default it uses ConstraintLayout. Whenever i am creating either Linear or Relative, I am not able to set the layout_width as well as layout_height as match_parent. Plz, help me to overcome this issue.

@nugoo1

This comment has been minimized.

Copy link

commented May 17, 2017

Hi boys and girls, saw that you had a problem. Try adding this block of java code and see if it works! (I modified it from Katherine's past lecture)

private void display(int number) {
TextView team_a_score = (TextView) findViewById(
R.id.team_a_score);
team_a_score.setText("" + number);

}
@ghost

This comment has been minimized.

Copy link

commented May 21, 2017

My Java code gives me this error message on the third line of the code i have pasted "cannot resolve symbol R". I have cleaned and rebuilt the project under build option, i have synced project with gradle files under tools option, i have synchronized the app. I have built the app from scratch and still have this error. Please someone help

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

}
@geekbae

This comment has been minimized.

Copy link

commented Jun 13, 2017

works perfectly fine however I never initialed score to 0 but it starts at 0 How?

Preview shows nothing but actual phone starts at 0 ?

`package com.example.android.courtcounter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
int score;

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

public void team_3_score (View view) {
    score = (score + 3);
    displayForTeamA(score);
}

public void team_2_score (View view) {
    score = (score + 2);
    displayForTeamA(score);
}

public void team_1_score (View view) {
    score = (score + 1);
    displayForTeamA(score);
}
/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score ) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}

}`

xml code for the button, calls that Java method

<TextView
android:id="@+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="16dp"
android:text="" // no value assigned either/
android:textColor="@color/colorPrimary"
android:textSize="32dp" />

@satyaki07

This comment has been minimized.

Copy link

commented Jul 1, 2017

only the onCreate method is available in my MainActivity.java, the other methods are not available.

@amelenp

This comment has been minimized.

Copy link

commented Jul 15, 2017

slickr1ck i faced the same problem, can you tell me how did you solve the problem please :D

@ghost

This comment has been minimized.

Copy link

commented Dec 17, 2017

Let's copy and paste it in!!

@alansvits

This comment has been minimized.

Copy link

commented Dec 24, 2017

Так багато подяк. Чи став хтось андроiд розробником пiсля цього курсу? тобто отримав роботу.

@lann22

This comment has been minimized.

Copy link

commented Jan 7, 2018

clear

@dennisotugo

This comment has been minimized.

Copy link

commented Jan 25, 2018

dab dab dab dab

@Maxwe1

This comment has been minimized.

Copy link

commented Feb 1, 2018

Very Clear.

@abdullahelsheikh

This comment has been minimized.

Copy link

commented Feb 6, 2018

thanks

@Nogaeman

This comment has been minimized.

Copy link

commented Feb 6, 2018

This Code
1
And This is Result
untitled

@mamdogogo

This comment has been minimized.

Copy link

commented Feb 12, 2018


package com.example.android.courtcounter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

int score = 0;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}
/**

  • Displays the given score for Team A.

*/
public void team_3_score (View view) {
score = (score + 3);
display (score);
}

public void team_2_score (View view) {
score = (score + 2);
display (score);
}

public void team_1_score (View view) {
score = (score + 1);
display (score);
}

private void display(int number) {
TextView quantityTextView = (TextView)
findViewById(R.id.team_a_score);
quantityTextView.setText("" + number);
}

}

screenshot_20180212-061521

@abdahma01

This comment has been minimized.

Copy link

commented Feb 14, 2018

untitled

@badrddinb

This comment has been minimized.

Copy link

commented Feb 22, 2018

DONE
image

@kkdroidgit

This comment has been minimized.

Copy link

commented Mar 23, 2018

No need to cast View type explicitly from API level 26, this should also work

/**
  * Displays the given score for Team A.
  */
 public void displayForTeamA(int score) {
   TextView scoreView = findViewById(R.id.team_a_score);
   scoreView.setText(String.valueOf(score));
 }
@adithikrishnan

This comment has been minimized.

Copy link

commented Apr 11, 2018

amelenp, Even i have the same error...could you help me out?

@hudsonpereira

This comment has been minimized.

Copy link

commented May 6, 2018

Thanks

@hortontan

This comment has been minimized.

Copy link

commented May 8, 2018

package com.example.android.courtcounter;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

/**
 * Increase the score for Team A by 1 point.
 */
public void addOneForTeamA(View v) {
    displayForTeamA(1);
}

/**
 * Increase the score for Team A by 2 points.
 */
public void addTwoForTeamA(View v) {
    displayForTeamA(2);
}

/**
 * Increase the score for Team A by 3 points.
 */
public void addThreeForTeamA(View v) {
    displayForTeamA(3);
}

/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}
@sneha123

This comment has been minimized.

Copy link

commented May 14, 2018

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="TEAM A"
    android:gravity="center_horizontal"
    android:layout_margin="14dp"/>

<TextView
    android:id="@+id/team_a_score"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:gravity="center_horizontal"
    android:text="0" />


<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="+3 points"
    android:onClick="addThreeForTeamA"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="+2 points"
    android:onClick="addTwoForTeamA"/>


<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:text="Free throw"
    android:onClick="addOneForTeamA"/>
<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="wrap_content"
    >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TEAM B"
        android:gravity="center_horizontal"
        android:layout_margin="14dp"/>

    <TextView
        android:id="@+id/team_b_score"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:gravity="center_horizontal"
        android:text="0" />


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="+3 points"
        android:onClick="addThreeForTeamB"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="+2 points"
        android:onClick="addTwoForTeamB"/>


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Free throw"
        android:onClick="addOneForTeamB"/>


</LinearLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    android:text="Reset" />

How to add reset button in middle

@araby123

This comment has been minimized.

Copy link

commented May 23, 2018

its working :)
screen shot 2018-05-24 at 2 32 02 am

@yasoamro

This comment has been minimized.

Copy link

commented May 30, 2018

Thanks

@ayatbahaa96

This comment has been minimized.

Copy link

commented Jun 1, 2018

xml code :

<TextView
    android:id="@+id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Team A"
    android:textAlignment="center"
    android:layout_marginBottom="16dp"
    android:textSize="36sp"
    android:textColor="#000000"/>

<TextView
    android:id="@+id/text2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
   android:textAlignment="center"
    android:text="@string/_0"
    tools:ignore="InvalidId"
    android:layout_marginBottom="16dp"
    android:textColor="#000000"
    android:textSize="36dp"/>


<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:text="+3 points"
    android:textAllCaps="true"
    android:textSize="20sp"
    android:onClick="Button1"
    android:background="#000000"
    android:textColor="#ffffff"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:text="+2 points"
    android:textAllCaps="true"
    android:textSize="20sp"
    android:onClick="Button2"
    android:background="#000000"
    android:textColor="#ffffff"
    />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    android:layout_marginBottom="16dp"

    android:text="Free throw"
    android:textAllCaps="true"
    android:textSize="20sp"
    android:onClick="Button3"
    android:background="#000000"
    android:textColor="#ffffff"
    />

java code
image

execute
image

@tooptooptoop

This comment has been minimized.

Copy link

commented Jun 3, 2018

screenshot_2018-06-03-18-26-46 1package com.example.android.courtcounter;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayForTeamA(8);
}
/**

  • Displays the given score for Team A.
    */
    public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
    }
    }

@tooptooptoop

This comment has been minimized.

Copy link

commented Jun 3, 2018

screenshot_2018-06-03-19-25-33 1
package com.example.android.courtcounter;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    displayForTeamA(8);
}
/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}

public void Button1(View view){
    displayForTeamA(3);
}
public void Button2(View view){
    displayForTeamA(2);
}
public void Button3(View view){
    displayForTeamA(1);
}

}

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="4dp"
    android:text="Team A"/>


<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:padding="4dp"
    android:text="0"
    android:id="@+id/team_a_score"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="+3 Points"
    android:id="@+id/Button1"
    android:onClick="Button1"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="+2 Points"
    android:id="@+id/Button2"
    android:onClick="Button2"/>

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:text="Free throw"
    android:id="@+id/Button3"
    android:onClick="Button3"/>
@ahmethalici

This comment has been minimized.

Copy link

commented Jul 29, 2018

@TheEndIsNear
No this should be public because we will try to reach this method from xml

@Dileep124

This comment has been minimized.

Copy link

commented Aug 29, 2018

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ':app:mergeDebugResources'.

C:\Users\dilee\AndroidStudioProjects\FirstAPP\app\src\main\res\values\strings.xml: Error: Found item String/enter_a_number more than one time

  • Try:
    Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

  • Get more help at https://help.gradle.org

BUILD FAILED in 1s

@Dileep124

This comment has been minimized.

Copy link

commented Sep 17, 2018

could you help me please

09/17 20:39:19: Launching app
$ adb shell am start -n "com.example.dilee.calculater/com.example.dilee.calculater.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 31260 on device oppo-cph1609-JRKFIJWOMZZDSSFU
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dilee.calculater, PID: 31260
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.Button.getText()' on a null object reference
at com.example.dilee.calculater.MainActivity$3.onClick(MainActivity.java:78)
at android.view.View.performClick(View.java:5290)
at android.view.View$PerformClick.run(View.java:21603)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:238)
at android.app.ActivityThread.main(ActivityThread.java:6006)
Application terminated.

@tko8567

This comment has been minimized.

Copy link

commented Oct 5, 2018

could you help me please

09/17 20:39:19: Launching app
$ adb shell am start -n "com.example.dilee.calculater/com.example.dilee.calculater.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 31260 on device oppo-cph1609-JRKFIJWOMZZDSSFU
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dilee.calculater, PID: 31260
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.Button.getText()' on a null object reference
at com.example.dilee.calculater.MainActivity$3.onClick(MainActivity.java:78)
at android.view.View.performClick(View.java:5290)
at android.view.View$PerformClick.run(View.java:21603)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:238)
at android.app.ActivityThread.main(ActivityThread.java:6006)
Application terminated.

Show please your MainActivity.java file. Exception occurs at line 78. As I can see, you try the method getText() on some button which wasn't initialized, being null
Maybe you have forgotten to call findViewById(int reference) to initialize the variable

@mcface3000

This comment has been minimized.

Copy link

commented Nov 2, 2018

So, after creating the recommended resource file (menu) and creating menu_main (xml resource file), the final clincher for me was adding this to that xml file:

<item android:id="@+id/action_settings"
    android:title="Action Settings" />/>

Which I got from here:
https://code.tutsplus.com/tutorials/android-sdk-implement-an-options-menu--mobile-9453

@mohkatta

This comment has been minimized.

Copy link

commented Aug 14, 2019

finally I did
Activity_Main.Xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentLeft="true"
    android:layout_marginStart="0dp"
    android:layout_marginLeft="0dp"
    android:layout_marginTop="16dp"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation='vertical'
        tools:context='.MainActivity'>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-medium"
            android:gravity="center"
            android:padding="16dp"
            android:text="Team A"
            android:textColor="#616161"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/team_a_score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-black"
            android:gravity="center"
            android:padding="24dp"
            android:text="0"
            android:textColor="#000000"
            android:textSize="56sp" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="24dp"
            android:onClick="addThreeForTeamA"
            android:text="+3 Points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="24dp"
            android:onClick="addTwoForTeamA"
            android:text="+2 Points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="24dp"
            android:onClick="addOneForTeamA"
            android:text="Free throw" />
    </LinearLayout>

    view
        android:layout_width="10dp"
        android:layout_height="match_parent"
        android:background="@android:color/black"

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation='vertical'
        tools:context='.MainActivity'>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-medium"
            android:gravity="center"
            android:padding="16dp"
            android:text="Team B"
            android:textColor="#616161"
            android:textSize="20sp" />
        <TextView
            android:id="@+id/team_b_score"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="sans-serif-black"
            android:gravity="center"
            android:padding="24dp"
            android:text="0"
            android:textColor="#000000"
            android:textSize="56sp" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="24dp"
            android:onClick="addThreeForTeamB"
            android:text="+3 Points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="24dp"
            android:onClick="addTwoForTeamB"
            android:text="+2 Points" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="24dp"
            android:onClick="addOneForTeamB"
            android:text="Free throw" />
    </LinearLayout>
</LinearLayout>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginLeft="22dp"
    android:layout_marginRight="22dp"
    android:layout_marginBottom="8dp"
    android:onClick="ResetScore"
    android:text="Reset" />

MainActivity.Java

package android.example.com;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
int scoreTeamA = 0;
int scoreTeamB = 0;

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

/**
 * Increase the score for Team A by 1 point.
 */
public void addOneForTeamA(View v) {
    scoreTeamA = scoreTeamA + 1;
    displayForTeamA(scoreTeamA);
}

/**
 * Increase the score for Team A by 2 points.
 */
public void addTwoForTeamA(View v) {
    scoreTeamA = scoreTeamA + 2;
    displayForTeamA(scoreTeamA);
}

/**
 * Increase the score for Team A by 3 points.
 */
public void addThreeForTeamA(View v) {
    scoreTeamA = scoreTeamA + 3;
    displayForTeamA(scoreTeamA);
}

/**
 * Increase the score for Team B by 1 point.
 */
public void addOneForTeamB(View v) {
    scoreTeamB = scoreTeamB + 1;
    displayForTeamB(scoreTeamB);
}

/**
 * Increase the score for Team B by 2 points.
 */
public void addTwoForTeamB(View v) {
    scoreTeamB = scoreTeamB + 2;
    displayForTeamB(scoreTeamB);
}

/**
 * Increase the score for Team B by 3 points.
 */
public void addThreeForTeamB(View v) {
    scoreTeamB = scoreTeamB + 3;
    displayForTeamB(scoreTeamB);
}

/**
 * Reset the score for both teams back to 0
 *
 * @param v
 */
public void ResetScore(View v) {
    scoreTeamA = 0;
    scoreTeamB = 0;
    displayForTeamA(scoreTeamA);
    displayForTeamB(scoreTeamB);
}

/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_a_score);
    scoreView.setText(String.valueOf(score));
}

/**
 * Displays the given score for Team B.
 */
public void displayForTeamB(int score) {
    TextView scoreView = (TextView) findViewById(R.id.team_b_score);
    scoreView.setText(String.valueOf(score));
}

}

style.xml

<style name="AppTheme" parent="Theme.AppCompat.Light"> #FF9800 #FF9800 </style>
@hamada3a3

This comment has been minimized.

Copy link

commented Aug 20, 2019

شكرا جزيلا

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.