2017-06-29 118 views
0

我想在java中創建自己的類,但是我遇到了問題和錯誤。我應該能夠創建一個簡單的類並在主要活動公共類中使用它。這裏是我的代碼:Java Android - 創建我自己的類

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/[email protected][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/[email protected][MainActivity]: setView = [email protected][MainActivity] touchMode=true 
06-29 12:21:53.566 7188-7188/com.example.android.scoreapp D/[email protected][MainActivity]: dispatchAttachedToWindow 
06-29 12:21:53.618 7188-7188/com.example.android.scoreapp D/[email protected][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/[email protected][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/[email protected][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/[email protected][MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1 
06-29 12:21:54.010 7188-7188/com.example.android.scoreapp D/[email protected][MainActivity]: mHardwareRenderer.initializeIfNeeded()#2 mSurface={isValid=true 513479648768} 
06-29 12:21:54.011 7188-7188/com.example.android.scoreapp V/InputMethodManager: Starting input: [email protected] 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: [email protected] nm : com.example.android.scoreapp ic=null 
06-29 12:21:59.328 7188-7188/com.example.android.scoreapp D/[email protected][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/[email protected][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)  

回答

1

在渲染方法,你是給整數值的setText創建一個名爲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類()。所以框架認爲它是一個資源ID。然後它找不到它。這導致異常。

將這些值轉換爲字符串。

public void render() { 
    ... 
    redskinsScoreView.setText(String.valueOf(redSkins.score)); 
    cowboysScoreView.setText(String.valueOf(cowBoys.score)); 
} 
+0

謝謝!我會嘗試這個選項 – ryanwaite28

+0

這不是選項。這是你的問題原因:)你可以接受答案後,你嘗試 –

+0

謝謝,它的作品! – ryanwaite28

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 

最好的辦法是尋找引起日誌中的語句。如果我能看到完整的代碼,我可能會看到更多。

+0

謝謝!我會嘗試這個選項 – ryanwaite28

0

這樣

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

謝謝!我會嘗試這個選項 – ryanwaite28

+0

它應該給出同樣的錯誤,調用setText(0)是問題。資源使用整數 –