Java Android - 创建我自己的类

时间:2017-06-29 16:31:48

标签: java android

我想在java中创建自己的类,但我遇到了问题和错误。我应该能够创建一个简单的类并在主活动公共类中使用它。这是我的代码:

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

但是我收到了这个错误:

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) 

3 个答案:

答案 0 :(得分:1)

问题不在于您的FootballTeam课程,而在于您的onClick方法。似乎附加了点击侦听器的视图已经确定了不正确的ID。从日志中的这些行清楚可见: -

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

获取错误的最佳方法是在日志中查找Caused By语句。如果我能看到你的完整代码,我可能会看到更多。

答案 1 :(得分:1)

在render方法中,您给setText()赋予整数值。所以框架认为它是一个资源ID。然后它找不到它。这导致异常。

将这些值转换为String。

pip2.7 install pyOpenSSL

答案 2 :(得分:0)

创建一个名为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;
}   
}

在您的MainActivity中,您可以以这种方式使用FootballTeam类

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