2012-02-12 36 views
5

Tôi nhận được thông báo lỗi khi cố gắng chạy Dự án của mình. Tôi muốn tạo ra Tic Tac Toe cho Android, và tôi sử dụng tùy chỉnh Xem dưới đây để tạo Tic Tac Toe board:android.view.InflateException: Dòng tệp XML nhị phân # 7: Lỗi khi tăng cấp lớp

package org.me.TicTacToe.ui; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.Paint.Style; 
import android.view.MotionEvent; 
import android.view.View; 

public class TicTacToeBoard extends View { 

    private Tile[][] tile = null; //Abstract class to create tiles in Tic Tac Toe Board 
    int boardWidth = 3; //It mean Tic Tac Toe board is consists of 3x3 tiles 
    private int width; 
    private int height; 
    private Paint brush; 

    public TicTacToeBoard(Context context) { 
     super(context); 

     brush = new Paint(); 
     this.brush.setARGB(255, 0, 0, 0); 
     this.brush.setAntiAlias(true); 
     this.brush.setStyle(Style.STROKE); 
     this.brush.setStrokeWidth(5); 

     width = this.getWidth(); 
     height = this.getHeight(); 

     initBoard(); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     for (int i = 0; i < tile.length; i++) { 
      for (int j = 0; j < tile[0].length; j++) { 
       tile[i][j].draw(canvas, getResources(), j, i, 
        (this.getWidth() + 3)/tile.length, 
        this.getHeight()/tile[0].length); 
      } 
     } 

     int xs = this.getWidth()/boardWidth; 
     int ys = this.getHeight()/boardWidth; 
     for (int i = 0; i <= boardWidth; i++) { 
      canvas.drawLine(xs * i, 0, xs * i, this.getHeight(), brush); 
     } 
     for (int i = 0; i <= boardWidth; i++) { 
      canvas.drawLine(0, ys * i, this.getWidth(), ys * i, brush); 
     } 

     super.onDraw(canvas); 
    } 

    public void initBoard(){ 
     tile = new Tile[boardWidth][boardWidth]; 

     int xss = width/boardWidth; 
     int yss = height/boardWidth; 

     for (int row = 0; row < boardWidth; row++) { 
      for (int colmn = 0; colmn < boardWidth; colmn++) { 
       tile[row][colmn] = new TileEmpty(xss * colmn, row * yss); 
       // TileEmpty is extend class from Tile. 
       // TileEmpty represent empty tile in Tic Tac Toe 
      } 
     } 
    } 
} 

Tiếp theo, tôi đặt nó trong cách bố trí hoạt động dựa trên XML như thế này:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <org.me.TicTacToe.ui.TicTacToeBoard 
     android:id="@+id/game1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

</LinearLayout> 

Vì vậy, tôi nhận được lỗi ngoại lệ nghiêm trọng trong cửa sổ Eclipse LogCat của tôi:

AndroidRuntime(329): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.me.TicTacToe/org.me.TicTacToe.ui.TicTacToeActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class org.me.TicTacToe.ui.TicTacToeBoard 

Cách khắc phục?

dòng Full Logcat:

02-12 10:22:31.989: D/AndroidRuntime(329): Shutting down VM 
02-12 10:22:31.989: W/dalvikvm(329): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
02-12 10:22:32.008: E/AndroidRuntime(329): FATAL EXCEPTION: main 
02-12 10:22:32.008: E/AndroidRuntime(329): java.lang.RuntimeException: 
        Unable to start activity ComponentInfo{org.me.TicTacToe/org.rme.TicTacToe.ui.TicTacToeActivity}: 
        android.view.InflateException: Binary XML file line #18: Error inflating class org.me.TicTacToe.ui.TicTacToeBoard 
02-12 10:22:32.008: E/AndroidRuntime(329): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
02-12 10:22:32.008: E/AndroidRuntime(329): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
02-12 10:22:32.008: E/AndroidRuntime(329): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
02-12 10:22:32.008: E/AndroidRuntime(329): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
02-12 10:22:32.008: E/AndroidRuntime(329): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-12 10:22:32.008: E/AndroidRuntime(329): at android.os.Looper.loop(Looper.java:123) 
02-12 10:22:32.008: E/AndroidRuntime(329): at android.app.ActivityThread.main(ActivityThread.java:3683) 
02-12 10:22:32.008: E/AndroidRuntime(329): at java.lang.reflect.Method.invokeNative(Native Method) 
02-12 10:22:32.008: E/AndroidRuntime(329): at java.lang.reflect.Method.invoke(Method.java:507) 
02-12 10:22:32.008: E/AndroidRuntime(329): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
02-12 10:22:32.008: E/AndroidRuntime(329): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
02-12 10:22:32.008: E/AndroidRuntime(329): at dalvik.system.NativeStart.main(Native Method) 
02-12 10:22:32.008: E/AndroidRuntime(329): Caused by: android.view.InflateException: Binary XML file line #7: 
        Error inflating class org.me.TicTacToe.ui.TicTacToeBoard 
02-12 10:22:32.008: E/AndroidRuntime(329): at android.view.LayoutInflater.createView(LayoutInflater.java:508) 
02-12 10:22:32.008: E/AndroidRuntime(329): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:570) 
02-12 10:22:32.008: E/AndroidRuntime(329): at android.view.LayoutInflater.rInflate(LayoutInflater.java:623) 
02-12 10:22:32.008: E/AndroidRuntime(329): at android.view.LayoutInflater.inflate(LayoutInflater.java:408) 
02-12 10:22:32.008: E/AndroidRuntime(329): at android.view.LayoutInflater.inflate(LayoutInflater.java:320) 
02-12 10:22:32.008: E/AndroidRuntime(329): at android.view.LayoutInflater.inflate(LayoutInflater.java:276) 
02-12 10:22:32.008: E/AndroidRuntime(329): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207) 
02-12 10:22:32.008: E/AndroidRuntime(329): at android.app.Activity.setContentView(Activity.java:1657) 
02-12 10:22:32.008: E/AndroidRuntime(329): at org.me.TicTacToe.ui.TicTacToeActivity.onCreate(TicTacToeActivity.java:24) 
02-12 10:22:32.008: E/AndroidRuntime(329): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-12 10:22:32.008: E/AndroidRuntime(329): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
02-12 10:22:32.008: E/AndroidRuntime(329): ... 11 more 
02-12 10:22:32.008: E/AndroidRuntime(329): Caused by: java.lang.NoSuchMethodException: TicTacToeBoard(Context,AttributeSet) 
02-12 10:22:32.008: E/AndroidRuntime(329): at java.lang.Class.getMatchingConstructor(Class.java:643) 
02-12 10:22:32.008: E/AndroidRuntime(329): at java.lang.Class.getConstructor(Class.java:472) 
02-12 10:22:32.008: E/AndroidRuntime(329): at android.view.LayoutInflater.createView(LayoutInflater.java:480) 
02-12 10:22:32.008: E/AndroidRuntime(329): ... 21 more 
+0

Xin vui lòng, chỉ cho chúng ta nhiều dòng logcat của bạn. Tôi tin rằng lý do của vụ tai nạn được liệt kê trước đây, ở đâu đó trong một nhà xây dựng hội đồng quản trị. – Olegas

+0

@Olegas, đây là các dòng logcat đầy đủ của tôi. Tôi đã thêm nó vào bài đăng đầu tiên của tôi – Spongeboss

Trả lời

27

Bạn đang thiếu các nhà xây dựng cho TicTacToeBoard(Context, Attributeset). xem ví dụ this question.

EDIT: Đó là ngay trong LogCat bạn chỉ cần gửi:

Caused by: java.lang.NoSuchMethodException: TicTacToeBoard(Context,AttributeSet) 
+0

cảm ơn vì câu trả lời của bạn, nó thực sự hoạt động! Nó kết thúc đau đầu 2 ngày của tôi – Spongeboss

+2

@Spongeboss vui lòng chấp nhận nó là câu trả lời đúng. thnx ... nó làm việc :) –

+0

+1 cho yêu cầu xem mệnh đề "Gây ra bởi". Của tôi là "ClassNotFound Exception" và cách nguyên nhân tôi đã viết sai tên lớp của tôi –

Các vấn đề liên quan