android--exercise1-compass

书上的一个例子,一步一步按照树上的做但是还是出现了一点小错误:在catlog中:error openging trace file:No such file or directory(2) 指的是我自定义的view类,并且有inflate错误

03-12 02:09:57.254: D/AndroidRuntime(789): Shutting down VM
03-12 02:09:57.254: W/dalvikvm(789): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-12 02:09:57.304: E/AndroidRuntime(789): FATAL EXCEPTION: main
03-12 02:09:57.304: E/AndroidRuntime(789): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.compassapp/com.example.compassapp.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.compassapp.CompassView
03-12 02:09:57.304: E/AndroidRuntime(789):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-12 02:09:57.304: E/AndroidRuntime(789):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-12 02:09:57.304: E/AndroidRuntime(789):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-12 02:09:57.304: E/AndroidRuntime(789):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-12 02:09:57.304: E/AndroidRuntime(789):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 02:09:57.304: E/AndroidRuntime(789):  at android.os.Looper.loop(Looper.java:137)
03-12 02:09:57.304: E/AndroidRuntime(789):  at android.app.ActivityThread.main(ActivityThread.java:5041)
03-12 02:09:57.304: E/AndroidRuntime(789):  at java.lang.reflect.Method.invokeNative(Native Method)
03-12 02:09:57.304: E/AndroidRuntime(789):  at java.lang.reflect.Method.invoke(Method.java:511)
03-12 02:09:57.304: E/AndroidRuntime(789):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-12 02:09:57.304: E/AndroidRuntime(789):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-12 02:09:57.304: E/AndroidRuntime(789):  at dalvik.system.NativeStart.main(Native Method)
03-12 02:09:57.304: E/AndroidRuntime(789): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.compassapp.CompassView
03-12 02:09:57.304: E/AndroidRuntime(789):  at android.view.LayoutInflater.createView(LayoutInflater.java:596)
03-12 02:09:57.304: E/AndroidRuntime(789):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
03-12 02:09:57.304: E/AndroidRuntime(789):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-12 02:09:57.304: E/AndroidRuntime(789):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-12 02:09:57.304: E/AndroidRuntime(789):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-12 02:09:57.304: E/AndroidRuntime(789):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-12 02:09:57.304: E/AndroidRuntime(789):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
03-12 02:09:57.304: E/AndroidRuntime(789):  at android.app.Activity.setContentView(Activity.java:1881)
03-12 02:09:57.304: E/AndroidRuntime(789):  at com.example.compassapp.MainActivity.onCreate(MainActivity.java:12)
03-12 02:09:57.304: E/AndroidRuntime(789):  at android.app.Activity.performCreate(Activity.java:5104)
03-12 02:09:57.304: E/AndroidRuntime(789):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-12 02:09:57.304: E/AndroidRuntime(789):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-12 02:09:57.304: E/AndroidRuntime(789):  ... 11 more
03-12 02:09:57.304: E/AndroidRuntime(789): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
03-12 02:09:57.304: E/AndroidRuntime(789):  at java.lang.Class.getConstructorOrMethod(Class.java:460)
03-12 02:09:57.304: E/AndroidRuntime(789):  at java.lang.Class.getConstructor(Class.java:431)
03-12 02:09:57.304: E/AndroidRuntime(789):  at android.view.LayoutInflater.createView(LayoutInflater.java:561)
03-12 02:09:57.304: E/AndroidRuntime(789):  ... 22 more
03-12 02:10:21.435: E/Trace(810): error opening trace file: No such file or directory (2)
03-12 02:10:22.214: D/AndroidRuntime(810): Shutting down VM
03-12 02:10:22.214: W/dalvikvm(810): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-12 02:10:22.234: E/AndroidRuntime(810): FATAL EXCEPTION: main
03-12 02:10:22.234: E/AndroidRuntime(810): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.compassapp/com.example.compassapp.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.compassapp.CompassView
03-12 02:10:22.234: E/AndroidRuntime(810):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-12 02:10:22.234: E/AndroidRuntime(810):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-12 02:10:22.234: E/AndroidRuntime(810):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-12 02:10:22.234: E/AndroidRuntime(810):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-12 02:10:22.234: E/AndroidRuntime(810):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 02:10:22.234: E/AndroidRuntime(810):  at android.os.Looper.loop(Looper.java:137)
03-12 02:10:22.234: E/AndroidRuntime(810):  at android.app.ActivityThread.main(ActivityThread.java:5041)
03-12 02:10:22.234: E/AndroidRuntime(810):  at java.lang.reflect.Method.invokeNative(Native Method)
03-12 02:10:22.234: E/AndroidRuntime(810):  at java.lang.reflect.Method.invoke(Method.java:511)
03-12 02:10:22.234: E/AndroidRuntime(810):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-12 02:10:22.234: E/AndroidRuntime(810):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-12 02:10:22.234: E/AndroidRuntime(810):  at dalvik.system.NativeStart.main(Native Method)
03-12 02:10:22.234: E/AndroidRuntime(810): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.example.compassapp.CompassView
03-12 02:10:22.234: E/AndroidRuntime(810):  at android.view.LayoutInflater.createView(LayoutInflater.java:596)
03-12 02:10:22.234: E/AndroidRuntime(810):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
03-12 02:10:22.234: E/AndroidRuntime(810):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-12 02:10:22.234: E/AndroidRuntime(810):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-12 02:10:22.234: E/AndroidRuntime(810):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-12 02:10:22.234: E/AndroidRuntime(810):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-12 02:10:22.234: E/AndroidRuntime(810):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
03-12 02:10:22.234: E/AndroidRuntime(810):  at android.app.Activity.setContentView(Activity.java:1881)
03-12 02:10:22.234: E/AndroidRuntime(810):  at com.example.compassapp.MainActivity.onCreate(MainActivity.java:12)
03-12 02:10:22.234: E/AndroidRuntime(810):  at android.app.Activity.performCreate(Activity.java:5104)
03-12 02:10:22.234: E/AndroidRuntime(810):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-12 02:10:22.234: E/AndroidRuntime(810):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-12 02:10:22.234: E/AndroidRuntime(810):  ... 11 more
03-12 02:10:22.234: E/AndroidRuntime(810): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
03-12 02:10:22.234: E/AndroidRuntime(810):  at java.lang.Class.getConstructorOrMethod(Class.java:460)
03-12 02:10:22.234: E/AndroidRuntime(810):  at java.lang.Class.getConstructor(Class.java:431)
03-12 02:10:22.234: E/AndroidRuntime(810):  at android.view.LayoutInflater.createView(LayoutInflater.java:561)
03-12 02:10:22.234: E/AndroidRuntime(810):  ... 22 more

现贴出代码:

1。MainActivity

 1 package com.example.compassapp;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.view.Menu;
 6 
 7 public class MainActivity extends Activity {
 8 
 9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.main);
13         CompassView cv = (CompassView)findViewById(R.id.compassView);
14     }
15 
16     @Override
17     public boolean onCreateOptionsMenu(Menu menu) {
18         // Inflate the menu; this adds items to the action bar if it is present.
19         getMenuInflater().inflate(R.menu.main, menu);
20         return true;
21     }
22 
23 }

刚贴完这个发现怪不得初始化有问题。。。cv.setBearing(45)没写。。
2.compassView

  1 package com.example.compassapp;
  2 
  3 import android.os.Bundle;
  4 import android.app.Activity;
  5 import android.content.Context;
  6 import android.content.res.Resources;
  7 import android.graphics.Canvas;
  8 import android.graphics.Paint;
  9 import android.util.AttributeSet;
 10 import android.view.Menu;
 11 import android.view.View;
 12 
 13 public class CompassView extends View {
 14     private float bearing;
 15     private Paint makerPaint;
 16     private Paint textPaint;
 17     private Paint circlePaint;
 18     private String northString;
 19     private String eastString;
 20     private String southString;
 21     private String westString;
 22     private int textHeight;
 23     
 24     public void setBearing(float _bearing){
 25         bearing = _bearing;
 26     }
 27     
 28     public float getBearing(){
 29         return bearing;
 30     }
 31     public CompassView(Context context){
 32         super(context);
 33         initCompassView();
 34     }
 35     public CompassView(Context context, AttributeSet ats, int defaultStyle){
 36         super(context, ats, defaultStyle);
 37         initCompassView();
 38     }
 39     protected void initCompassView(){
 40         setFocusable(true);
 41         
 42         circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 43         Resources r = this.getResources();
 44         circlePaint.setColor(r.getColor(R.color.background_color));
 45         circlePaint.setStrokeWidth(1);
 46         circlePaint.setStyle(Paint.Style.FILL_AND_STROKE);
 47         northString = r.getString(R.string.cardinal_north);
 48         eastString = r.getString(R.string.cardinal_east);
 49         southString = r.getString(R.string.cardinal_south);
 50         westString = r.getString(R.string.cardinal_west);
 51         
 52         textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 53         textPaint.setColor(r.getColor(R.color.text_color));
 54         textHeight = (int)textPaint.measureText("yY");
 55         
 56         makerPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 57         makerPaint.setColor(r.getColor(R.color.marker_color));
 58         
 59     }
 60 
 61     @Override
 62     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
 63         int measureWidth = measure(widthMeasureSpec);
 64         int measureHeight = measure(heightMeasureSpec);
 65         int d = Math.min(measureWidth, measureHeight);
 66         setMeasuredDimension(d,d);
 67     }
 68     
 69     private int measure(int measureSpec){
 70         int result = 0;
 71         int specMode = MeasureSpec.getMode(measureSpec);
 72         int specSize = MeasureSpec.getSize(measureSpec);
 73         
 74         if (specMode == MeasureSpec.UNSPECIFIED){
 75             result = 200;
 76         } else{
 77             result = specSize;
 78         }
 79         return result;
 80     }
 81     
 82     @Override
 83     protected void onDraw(Canvas canvas){
 84         int px = getMeasuredWidth()/2;
 85         int py = getMeasuredHeight()/2;
 86         int radius = Math.min(px, py);
 87         canvas.drawCircle(px, py, radius, circlePaint);
 88         canvas.save();
 89         canvas.rotate(-bearing,px,py);
 90         
 91         int textWidth = (int)textPaint.measureText("W");
 92         int cardinalX = px - textWidth/2;
 93         int cardinalY = py - radius + textHeight;
 94         
 95         for(int i=0; i<24; i++){
 96             canvas.drawLine(px, py-radius, px, py-radius+10, makerPaint);
 97             canvas.save();
 98             canvas.translate(0, textHeight);
 99             
100             if (i%6 ==0){
101                 String dirString = "";
102                 switch (i) {
103                 case (0) :{
104                     dirString = northString;
105                     int arrowY = 2*textHeight;
106                     canvas.drawLine(px, arrowY, px-5, 3*textHeight, makerPaint);
107                     break;
108                 }
109                 case (6): dirString = eastString; break;
110                 case (12): dirString = southString; break;
111                 case (18): dirString = westString;break;
112                 }
113                 canvas.drawText(dirString, cardinalX, cardinalY, textPaint);
114             }
115             
116             else if (i % 3 == 0){
117                 String angle = String.valueOf(i*15);
118                 float angleTextWidth = textPaint.measureText(angle);
119                 
120                 int angleTextX = (int)(px - angleTextWidth/2);
121                 int angleTextY = py - radius + textHeight;
122                 canvas.drawText(angle, angleTextX, angleTextY, textPaint);
123             }
124             canvas.restore();
125             canvas.rotate(15,px,py);
126         }
127         canvas.restore();
128     }
129     
130     
131 
132 }

3.manifest

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.compassapp"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="17" />
10 
11     <application
12         android:allowBackup="true"
13         android:icon="@drawable/ic_launcher"
14         android:label="@string/app_name"
15         android:theme="@style/AppTheme" >
16         <activity
17             android:name="com.example.compassapp.MainActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25     </application>
26 
27 </manifest>

4.main.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:orientation="vertical"
 5     android:layout_width="fill_parent"
 6     android:layout_height="fill_parent">
 7 
 8     
 9     <com.example.compassapp.CompassView
10      xmlns:android = "http://schemas.android.com/apk/res/android"
11         android:id="@+id/compassView"
12         android:layout_width="fill_parent"
13         android:layout_height = "fill_parent"
14         />
15    
16 
17 </LinearLayout>

 最后将activity这样改,就好了。。。即使我使用下一行cv初始化将7,8行顺序呼唤都会出错nullpointerexception 为什么??

1 @Override
2     protected void onCreate(Bundle savedInstanceState) {
3         super.onCreate(savedInstanceState);
4         CompassView cv = new CompassView(this);
5 //        CompassView cv = (CompassView)findViewById(R.id.compassView);
6         
7         setContentView(cv);
8         cv.setBearing(45);

 虽然work了但是还是不甘。仔细看了catlog NoSuchMethodException:<init>[class android.Content.Context, interface android.util.AttribureSet]

所以是少了以context 和 attributeSet为参数的构造函数,加上

public CompassView(Context context, AttributeSet ats){
        super(context, ats);
        initCompassView();
    }

但是为什么不认我之前写的那两个构造函数呢?http://blog.csdn.net/z103594643/article/details/6755017

 

posted on 2013-03-12 10:31  lauraxia  阅读(503)  评论(0编辑  收藏  举报

导航