2017-09-13 74 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