Java Android - Create My Own Class

0

I want to create my own class in java but i'm having problems and errors. I should just be able to create a simple class and use it in the main activity public class. Here is my code:

package com.example.android.scoreapp;

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

class FootballTeam {
    String name;
    int score = 0;

    public FootballTeam(String name) {
        this.name = name;
    }

    public void touchDown(){
        this.score += 6;
    }

    public void fieldGoal(){
        this.score += 3;
    }

    public void twoPointConversion(){
        this.score += 2;
    }

    public void touchdownFieldGoal(){
        this.score += 1;
    }

    public void resetScore(){
        this.score = 0;
    }
}

public class MainActivity extends AppCompatActivity {

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

    /* --- Code --- */



    FootballTeam redSkins = new FootballTeam("Redskins");
    FootballTeam cowBoys = new FootballTeam("Cowboys");

    public void redSkins_touchDown(View view){
        redSkins.touchDown();
        render();
    }

    public void redSkins_fieldGoal(View view){
        redSkins.fieldGoal();
        render();
    }

    public void redSkins_twoPointConversion(View view){
        redSkins.twoPointConversion();
        render();
    }

    public void redSkins_touchdownFieldGoal(View view){
        redSkins.touchdownFieldGoal();
        render();
    }

    /* --- */

    public void cowBoys_touchDown(View view){
        cowBoys.touchDown();
        render();
    }

    public void cowBoys_fieldGoal(View view){
        cowBoys.fieldGoal();
        render();
    }

    public void cowBoys_twoPointConversion(View view){
        cowBoys.twoPointConversion();
        render();
    }

    public void cowBoys_touchdownFieldGoal(View view){
        cowBoys.touchdownFieldGoal();
        render();
    }

    /* --- */

    public void render() {
        TextView redskinsScoreView = (TextView) findViewById(R.id.redskinsscoreview);
        TextView cowboysScoreView = (TextView) findViewById(R.id.cowboysscoreview);
        redskinsScoreView.setText(redSkins.score);
        cowboysScoreView.setText(cowBoys.score);
    }

    public void resetScore(){
        redSkins.resetScore();
        cowBoys.resetScore();
        render();
    }
}

but i get this error:

06-29 12:21:53.029 7188-7188/? E/Zygote: v2
06-29 12:21:53.030 7188-7188/? I/libpersona: KNOX_SDCARD checking this for 10586
06-29 12:21:53.030 7188-7188/? I/libpersona: KNOX_SDCARD not a persona
06-29 12:21:53.030 7188-7188/? E/Zygote: accessInfo : 0
06-29 12:21:53.034 7188-7188/? W/SELinux: SELinux selinux_android_compute_policy_index : Policy Index[2],  Con:u:r:zygote:s0 RAM:SEPF_SECMOBILE_7.0_0005, [-1 -1 -4 -1 0 1]
06-29 12:21:53.035 7188-7188/? I/SELinux: SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=com.example.android.scoreapp 
06-29 12:21:53.037 7188-7188/? I/art: Late-enabling -Xcheck:jni
06-29 12:21:53.064 7188-7188/? D/TimaKeyStoreProvider: TimaKeyStore is not enabled: cannot add TimaSignature Service and generateKeyPair Service
06-29 12:21:53.182 7188-7188/com.example.android.scoreapp W/System: ClassLoader referenced unknown path: /data/app/com.example.android.scoreapp-1/lib/arm64
06-29 12:21:53.289 7188-7188/com.example.android.scoreapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-29 12:21:53.542 7188-7188/com.example.android.scoreapp D/ViewRootImpl@e324811[MainActivity]: ThreadedRenderer.create() translucent=false
06-29 12:21:53.547 7188-7188/com.example.android.scoreapp D/InputTransport: Input channel constructed: fd=58
06-29 12:21:53.548 7188-7188/com.example.android.scoreapp D/ViewRootImpl@e324811[MainActivity]: setView = DecorView@b840d76[MainActivity] touchMode=true
06-29 12:21:53.566 7188-7188/com.example.android.scoreapp D/ViewRootImpl@e324811[MainActivity]: dispatchAttachedToWindow
06-29 12:21:53.618 7188-7188/com.example.android.scoreapp D/ViewRootImpl@e324811[MainActivity]: Relayout returned: oldFrame=[0,0][0,0] newFrame=[0,0][1440,2560] result=0x7 surface={isValid=true 513479648768} surfaceGenerationChanged=true
06-29 12:21:53.618 7188-7188/com.example.android.scoreapp D/ViewRootImpl@e324811[MainActivity]: mHardwareRenderer.initialize() mSurface={isValid=true 513479648768} hwInitialized=true
06-29 12:21:53.700 7188-7195/com.example.android.scoreapp W/art: Suspending all threads took: 6.561ms
06-29 12:21:53.745 7188-7228/com.example.android.scoreapp D/libEGL: loaded /vendor/lib64/egl/libGLES_mali.so
06-29 12:21:53.771 7188-7228/com.example.android.scoreapp I/OpenGLRenderer: Initialized EGL, version 1.4
06-29 12:21:53.771 7188-7228/com.example.android.scoreapp D/OpenGLRenderer: Swap behavior 1
06-29 12:21:53.780 7188-7228/com.example.android.scoreapp D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000,  [1440x2560]-format:1
06-29 12:21:54.010 7188-7188/com.example.android.scoreapp D/ViewRootImpl@e324811[MainActivity]: MSG_RESIZED_REPORT: ci=Rect(0, 96 - 0, 0) vi=Rect(0, 96 - 0, 0) or=1
06-29 12:21:54.010 7188-7188/com.example.android.scoreapp D/ViewRootImpl@e324811[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1
06-29 12:21:54.010 7188-7188/com.example.android.scoreapp D/ViewRootImpl@e324811[MainActivity]: mHardwareRenderer.initializeIfNeeded()#2 mSurface={isValid=true 513479648768}
06-29 12:21:54.011 7188-7188/com.example.android.scoreapp V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@dca97e4 nm : com.example.android.scoreapp ic=null
06-29 12:21:54.011 7188-7188/com.example.android.scoreapp I/InputMethodManager: [IMM] startInputInner - mService.startInputOrWindowGainedFocus
06-29 12:21:54.028 7188-7201/com.example.android.scoreapp D/InputTransport: Input channel constructed: fd=69
06-29 12:21:54.039 7188-7188/com.example.android.scoreapp V/InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@7e2544d nm : com.example.android.scoreapp ic=null
06-29 12:21:59.328 7188-7188/com.example.android.scoreapp D/ViewRootImpl@e324811[MainActivity]: ViewPostImeInputStage processPointer 0
06-29 12:21:59.331 7188-7188/com.example.android.scoreapp W/System: ClassLoader referenced unknown path: /system/framework/QPerformance.jar
06-29 12:21:59.335 7188-7188/com.example.android.scoreapp E/BoostFramework: BoostFramework() : Exception_1 = java.lang.ClassNotFoundException: Didn't find class "com.qualcomm.qti.Performance" on path: DexPathList[[],nativeLibraryDirectories=[/system/lib64, /vendor/lib64]]
06-29 12:21:59.335 7188-7188/com.example.android.scoreapp V/BoostFramework: BoostFramework() : mPerf = null
06-29 12:21:59.384 7188-7188/com.example.android.scoreapp D/ViewRootImpl@e324811[MainActivity]: ViewPostImeInputStage processPointer 1
06-29 12:21:59.388 7188-7188/com.example.android.scoreapp W/ResourceType: No package identifier when getting value for resource number 0x00000006
06-29 12:21:59.390 7188-7188/com.example.android.scoreapp D/AndroidRuntime: Shutting down VM
06-29 12:21:59.395 7188-7188/com.example.android.scoreapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.example.android.scoreapp, PID: 7188
                                                                            java.lang.IllegalStateException: Could not execute method for android:onClick
                                                                                at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                                                                                at android.view.View.performClick(View.java:6205)
                                                                                at android.widget.TextView.performClick(TextView.java:11103)
                                                                                at android.view.View$PerformClick.run(View.java:23653)
                                                                                at android.os.Handler.handleCallback(Handler.java:751)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:154)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6682)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
                                                                             Caused by: java.lang.reflect.InvocationTargetException
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                                                                                at android.view.View.performClick(View.java:6205) 
                                                                                at android.widget.TextView.performClick(TextView.java:11103) 
                                                                                at android.view.View$PerformClick.run(View.java:23653) 
                                                                                at android.os.Handler.handleCallback(Handler.java:751) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                at android.os.Looper.loop(Looper.java:154) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6682) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
                                                                             Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x6
                                                                                at android.content.res.Resources.getText(Resources.java:1178)
                                                                                at android.widget.TextView.setText(TextView.java:5164)
                                                                                at com.example.android.scoreapp.MainActivity.render(MainActivity.java:99)
                                                                                at com.example.android.scoreapp.MainActivity.redSkins_touchDown(MainActivity.java:54)
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                                                                                at android.view.View.performClick(View.java:6205) 
                                                                                at android.widget.TextView.performClick(TextView.java:11103) 
                                                                                at android.view.View$PerformClick.run(View.java:23653) 
                                                                                at android.os.Handler.handleCallback(Handler.java:751) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                                at android.os.Looper.loop(Looper.java:154) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6682) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
java
android
asked on Stack Overflow Jun 29, 2017 by ryanwaite28

3 Answers

1

Problem is not with your FootballTeam class but with your onClick method. It seems the view on which click listeners are attached have been assingned incorrect id. It is clear from these lines in your log:-

java.lang.IllegalStateException: Could not execute method for android:onClick   
Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x6

Best way to get the error is look for Caused By statements in the log. I will probably be able to see more if I can see your full code.

answered on Stack Overflow Jun 29, 2017 by deepankar
1

In render method you are giving integer value to setText(). So framework thinks it is a resource id. Then it could not find it. This cause exception.

Cast these values to String.

public void render() {
    ...
    redskinsScoreView.setText(String.valueOf(redSkins.score));
    cowboysScoreView.setText(String.valueOf(cowBoys.score));
}
answered on Stack Overflow Jun 29, 2017 by Sinan Kozak
0

Create a separate class named FootballTeam

public class FootballTeam {

public static final int TOUCH_DOWN = 6;
public static final int GOAL = 3;

private String name;
private int score;

public FootballTeam(String name) {
    this.name = name;
}

public int getScore(){
return score;
}

public void setScore(int newScore){
this.score = newScore;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}   
}

In your MainActivity you can use you FootballTeam class in this way

public class MainActivity extends AppCompatActivity {

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

     FootballTeam redSkins = new FootballTeam("Redskins");
     FootballTeam cowBoys = new FootballTeam("Cowboys");

     // assign points
     redSkins.setScore(redSkins.getScore() + FootballTeam.TOUCH_DOWN);
     cowBoys.setScore(cowBoys.getScore() + FootballTeam.GOAL);

     //reset points
     redSkins.setScore(0);
}
}
answered on Stack Overflow Jun 29, 2017 by Giacomo Lai

User contributions licensed under CC BY-SA 3.0