2013-12-13 72 views
1

所以我一直在試圖從這本書中學習,並且我完全按照它的說法做了(至少我是這麼認爲的),但該應用程序並未顯示在AVD中。 我有2班在我的包名爲(com.example.crazyeights)CrazyEightsActivity.java & CrazyEightsView.javaCrazyEightsActivity,我有:程序化的Android應用程序不顯示在AVD中

package com.example.crazyeights; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 

public class CrazyEightsActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     CrazyEightsView CrazyEightsView = new CrazyEightsView(this); 
     setContentView(R.layout.activity_main); 
    } 
} 

而且CrazyEightsView.java下,我有:

package com.example.crazyeights; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.view.View; 

public class CrazyEightsView extends View 
{ 
    private Paint redPaint; 
    private int circleX; 
    private int circleY; 
    private float radius; 

public CrazyEightsView(Context context) { 
     super(context); 
     redPaint = new Paint(); 
     redPaint.setAntiAlias(true); 
     redPaint.setColor(Color.RED); 
     circleX = 100; 
     circleY = 100; 
     radius = 30; 
    } 
    @Override 
    protected void onDraw(Canvas canvas) 
    { 
     canvas.drawCircle(circleX, circleY, radius, redPaint); 
    } 

} 

我已經做了一個「測試-app「只是爲了看看它是否適用於我的AVD,它的作用。 感謝您的幫助!

編輯:我AndroidManifest.xml目前看起來是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.crazyeights" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="18" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.crazyeights.CrazyEightsActivity" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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

</manifest> 

回答

2

你也有清單文件?

如果你有一個清單你添加新的活動清單是這樣的:

<activity> 
     android:name="com.example.crazyeights.CrazyEightsActivity" 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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

我複製並將其替換爲androidManifest.xml中的部分但是,我收到一個錯誤:元素類型「activity」必須後跟任一屬性specifica tions,「>」或「/>」 – user2789610

+0

Put> After ... CrazyEightsActivity「 – Melquiades

+0

嗯...我做對了嗎? (我用我的.xml編輯了我原來的帖子) – user2789610

0

你必須在CrazyEightsActivity一些變化該類

1)OnCreateMethod

@Override  
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(new CrazyEightsView(CrazyEightsActivity.this)); 
} 
+0

「你必須在CrazyEightsActivity這個類上做些改變」這是什麼意思? – user2789610

+0

你的自定義視圖類的名稱是CrazyEightsView確定那麼你必須顯示這個視圖使用setcontentview(編號) CrazyEightsView crazyEightView = new CrazyEightsView(CrazyEightsActivity.this); setContentView(crazyEightView); //如果您必須參考示例im傳遞// stackoverflow鏈接 //檢查此鏈接以供參考 ** http://stackoverflow.com/questions/20513478/setcontentview-by-using-extending-view-view -類** – Sanket990

相關問題