2017-09-13 42 views
0
package com.harshil.home.tictac; 

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

public class MainActivity extends AppCompatActivity { 

int c[][]; 
int i, j, k = 0; 
Button b[][]; 
TextView textView; 
int player = 1; 

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

    setBoard(); 
} 


private void setBoard() { 
    b = new Button[4][4]; 
    c = new int[4][4]; 


    b[1][1] = (Button) findViewById(R.id.one); 
    b[1][2] = (Button) findViewById(R.id.two); 
    b[1][3] = (Button) findViewById(R.id.three); 

    b[2][1] = (Button) findViewById(R.id.four); 
    b[2][2] = (Button) findViewById(R.id.five); 
    b[2][3] = (Button) findViewById(R.id.six); 

    b[3][1] = (Button) findViewById(R.id.seven); 
    b[3][2] = (Button) findViewById(R.id.eight); 
    b[3][3] = (Button) findViewById(R.id.nine); 

    for (i = 1; i <= 3; i++) { 
     for (j = 1; j <= 3; j++) 
      c[i][j] = 2; 
    } 


    textView.setText("Click a button to start"); 
    //adding onclickevent for each buttons 
    for (i = 1; i <= 3; i++) { 
     for (j = 1; j <= 3; j++) { 
      b[i][j].setOnClickListener(new MyClickListener(i, j)); 
      if (!b[i][j].isEnabled()) { 
       b[i][j].setText(""); 
       b[i][j].setEnabled(true); 
      } 
     } 
     } 
    } 

    class MyClickListener implements View.OnClickListener { 
    int x; 
    int y; 

    public MyClickListener(int x, int y) { 
     this.x = x; 
     this.y = y; 
    } 


    public void onClick(View view) { 

     textView.setText("Another player's Turn"); 
     if ((b[x][y].isEnabled())) { 
      b[x][y].setEnabled(false); 
      b[x][y].setText("O"); 
      c[x][y] = 0; 
      textView.setText(""); 
      if (!checkBoard()) { 
       if ((b[x][y].isEnabled())) { 
        b[x][y].setEnabled(false); 
        b[x][y].setText("X"); 
        c[x][y] = 0; 
        textView.setText(""); 

       } 

      } 


      } 


     } 

     private boolean checkBoard() { 

     boolean gameOver = false; 
     if ((c[1][1] == 0 && c[2][2] == 0 && c[3][3] == 0) 
       || (c[1][3] == 0 && c[2][2] == 0 && c[3][1] == 0) 
       || (c[1][2] == 0 && c[2][2] == 0 && c[3][2] == 0) 
       || (c[1][3] == 0 && c[2][3] == 0 && c[3][3] == 0) 
       || (c[1][1] == 0 && c[1][2] == 0 && c[1][3] == 0) 
       || (c[2][1] == 0 && c[2][2] == 0 && c[2][3] == 0) 
       || (c[3][1] == 0 && c[3][2] == 0 && c[3][3] == 0) 
       || (c[1][1] == 0 && c[2][1] == 0 && c[3][1] == 0)) { 
      textView.setText("Game over. player 1 wins!"); 
      gameOver = true; 
     } else if ((c[1][1] == 1 && c[2][2] == 1 && c[3][3] == 1) 
       || (c[1][3] == 1 && c[2][2] == 1 && c[3][1] == 1) 
       || (c[1][2] == 1 && c[2][2] == 1 && c[3][2] == 1) 
       || (c[1][3] == 1 && c[2][3] == 1 && c[3][3] == 1) 
       || (c[1][1] == 1 && c[1][2] == 1 && c[1][3] == 1) 
       || (c[2][1] == 1 && c[2][2] == 1 && c[2][3] == 1) 
       || (c[3][1] == 1 && c[3][2] == 1 && c[3][3] == 1) 
       || (c[1][1] == 1 && c[2][1] == 1 && c[3][1] == 1)) { 
      textView.setText("Game over. player 2 wins!"); 
      gameOver = true; 
     } else { 
      boolean empty = false; 
      for (i = 1; i <= 3; i++) { 
       for (j = 1; j <= 3; j++) { 
        if (c[i][j] == 2) { 
         empty = true; 
         break; 
        } 
       } 
      } 
      if (!empty) { 
       gameOver = true; 
       textView.setText("Game over. It's a draw!"); 
      } 
     } 
     return gameOver; 
     } 


     } 
     } 

我試圖修改2玩家,但可悲的是, 構建成功,但仍然應用程序崩潰!我忘了更改AndroidManifest.xml文件中的任何內容嗎?E/AndroidRuntime:致命的例外:主要和崩潰

的誤差

09-13 21:26:37.043 3730-3730/? E/AndroidRuntime: FATAL EXCEPTION: main 
               Process: com.harshil.home.tictac, PID: 3730 
               java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.harshil.home.tictac/com.harshil.home.tictac.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.harshil.home.tictac.MainActivity" on path: DexPathList[[zip file "/data/app/com.harshil.home.tictac-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] 
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2236) 
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                at android.os.Handler.dispatchMessage(Handler.java:102) 
                at android.os.Looper.loop(Looper.java:135) 
                at android.app.ActivityThread.main(ActivityThread.java:5254) 
                at java.lang.reflect.Method.invoke(Native Method) 
                at java.lang.reflect.Method.invoke(Method.java:372) 
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
                Caused by: java.lang.ClassNotFoundException: Didn't find class "com.harshil.home.tictac.MainActivity" on path: DexPathList[[zip file "/data/app/com.harshil.home.tictac-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]] 
                at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) 
                at java.lang.ClassLoader.loadClass(ClassLoader.java:511) 
                at java.lang.ClassLoader.loadClass(ClassLoader.java:469) 
                at android.app.Instrumentation.newActivity(Instrumentation.java:1066) 
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2226) 
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)  
                at android.app.ActivityThread.access$800(ActivityThread.java:151)  
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)  
                at android.os.Handler.dispatchMessage(Handler.java:102)  
                at android.os.Looper.loop(Looper.java:135)  
                at android.app.ActivityThread.main(ActivityThread.java:5254)  
                at java.lang.reflect.Method.invoke(Native Method)  
                at java.lang.reflect.Method.invoke(Method.java:372)  
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)  
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)  
                Suppressed: java.lang.NoClassDefFoundError: com.harshil.home.tictac.MainActivity 
                at dalvik.system.DexFile.defineClassNative(Native Method) 
                at dalvik.system.DexFile.defineClass(DexFile.java:226) 
                at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:219) 
                at dalvik.system.DexPathList.findClass(DexPathList.java:321) 
                at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54) 
                  ... 14 more 
                Suppressed: java.lang.ClassNotFoundException: com.harshil.home.tictac.MainActivity 
                at java.lang.Class.classForName(Native Method) 
                at java.lang.BootClassLoader.findClass(ClassLoader.java:781) 
                at java.lang.BootClassLoader.loadClass(ClassLoader.java:841) 
                at java.lang.ClassLoader.loadClass(ClassLoader.java:504) 
                  ... 13 more 
                Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available 
09-13 21:26:39.082 3730-3730/? I/Process: Sending signal. PID: 3730 SIG: 9 

清單文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.harshil.home.tictac"> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
     </activity> 
     </application> 

     </manifest> 
+0

我爲新,所以只是想暗示是否我在主要活動中提到的類不會在相同的Java類中工作?我是否需要爲每個功能創建不同的活動? – Maximus

+0

檢查軟件包名稱 – MeknessiHamida

+0

我把androidmanifest.xml中的所有東西都保存爲默認值,那是錯誤的嗎? – Maximus

回答

0
package com.harshil.home.tictac; 

import android.app.Activity; 
import android.content.Context; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

int c[][]; 
int i, j, k = 0; 
Button b[][]; 
TextView textView; 
int player=2; 


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

    setBoard(); 
} 


private void setBoard() { 
    b = new Button[4][4]; 
    c = new int[4][4]; 


    b[1][1] = (Button) findViewById(R.id.one); 
    b[1][2] = (Button) findViewById(R.id.two); 
    b[1][3] = (Button) findViewById(R.id.three); 

    b[2][1] = (Button) findViewById(R.id.four); 
    b[2][2] = (Button) findViewById(R.id.five); 
    b[2][3] = (Button) findViewById(R.id.six); 

    b[3][1] = (Button) findViewById(R.id.seven); 
    b[3][2] = (Button) findViewById(R.id.eight); 
    b[3][3] = (Button) findViewById(R.id.nine); 

    for (i = 1; i <= 3; i++) { 
     for (j = 1; j <= 3; j++) 
      c[i][j] = 2; 
    } 



    //adding onclickevent for each buttons 
    for (i = 1; i <= 3; i++) { 
     for (j = 1; j <= 3; j++) { 
      b[i][j].setOnClickListener(new MyClickListener(i, j)); 
      if (!b[i][j].isEnabled()) { 
       b[i][j].setText(""); 
       b[i][j].setEnabled(true); 
      } 
     } 
     } 
    } 
    class MyClickListener implements View.OnClickListener { 
    int x; 
    int y; 

    public MyClickListener(int x, int y) { 
     this.x = x; 
     this.y = y; 
    } 





     public void onClick(View view) { 

      if (player==2){ 
      if (b[x][y].isEnabled()) { 
       b[x][y].setEnabled(false); 
       b[x][y].setText("O"); 
       c[x][y] = 0; 
       player++; 
       if (checkBoard()){ 

        player=4; 
        Toast.makeText(getApplicationContext(),"player O 
       won",Toast.LENGTH_SHORT).show(); 


       } 

      } 
      } 
      if(player==3){ 
       if (b[x][y].isEnabled()) { 
        b[x][y].setEnabled(false); 
        b[x][y].setText("X"); 
        c[x][y] = 1; 

        player=2; 
        if (checkBoard()){ 

         player=5; 
         Toast.makeText(getApplicationContext(),"player X 
        won",Toast.LENGTH_SHORT).show(); 

        } 


       } 
       } 


      } 




     } 




      private boolean checkBoard() { 

      boolean gameOver = false; 
      if ((c[1][1] == 0 && c[2][2] == 0 && c[3][3] == 0) 
       || (c[1][3] == 0 && c[2][2] == 0 && c[3][1] == 0) 
       || (c[1][2] == 0 && c[2][2] == 0 && c[3][2] == 0) 
       || (c[1][3] == 0 && c[2][3] == 0 && c[3][3] == 0) 
       || (c[1][1] == 0 && c[1][2] == 0 && c[1][3] == 0) 
       || (c[2][1] == 0 && c[2][2] == 0 && c[2][3] == 0) 
       || (c[3][1] == 0 && c[3][2] == 0 && c[3][3] == 0) 
       || (c[1][1] == 0 && c[2][1] == 0 && c[3][1] == 0)) { 

      gameOver = true; 
      } else if ((c[1][1] == 1 && c[2][2] == 1 && c[3][3] == 1) 
       || (c[1][3] == 1 && c[2][2] == 1 && c[3][1] == 1) 
       || (c[1][2] == 1 && c[2][2] == 1 && c[3][2] == 1) 
       || (c[1][3] == 1 && c[2][3] == 1 && c[3][3] == 1) 
       || (c[1][1] == 1 && c[1][2] == 1 && c[1][3] == 1) 
       || (c[2][1] == 1 && c[2][2] == 1 && c[2][3] == 1) 
       || (c[3][1] == 1 && c[3][2] == 1 && c[3][3] == 1) 
       || (c[1][1] == 1 && c[2][1] == 1 && c[3][1] == 1)) { 

      gameOver = true; 
      } else { 
      boolean empty = false; 
      for (i = 1; i <= 3; i++) { 
       for (j = 1; j <= 3; j++) { 
        if (c[i][j] == 2) { 
         empty = true; 
         break; 
        } 
       } 
      } 
      if (!empty) { 
       gameOver = true; 
       Toast.makeText(getApplicationContext(),"Game 
      Drawn",Toast.LENGTH_SHORT).show(); 

      } 
      } 
     return gameOver; 
     } 


     } 

終於得到了它的工作謝謝所有

+0

恭喜!現在您可以接受您的答案爲已接受的答案來關閉此主題。 –

+1

Android真的很有趣!希望我很快適應! – Maximus