开发者

Draw line in tab

开发者 https://www.devze.com 2023-03-10 02:06 出处:网络
I want to draw some lines into second tab, but I had error \"...stop unexpectedly\". Maybe someone can help...

I want to draw some lines into second tab, but I had error "...stop unexpectedly". Maybe someone can help...

tab_map.xml

    <?xml version="1.0" encoding="utf-8"?>
<FrameLayout
  android:id="@+id/map_view"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#FFFFFF" />

TabMap.java

public class TabMap extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_map);

        FrameLayout fLayout = (FrameLayout) findViewById(R.id.map_view);
        fLayout.addView(new TabMapLine(this, 0, 0, 50, 50));
    }

TabMapLine.java

public class TabMapLine extends View{

    private final float startX;
    private final float stopX;
    private final float startY;
    private final float stopY;
    Paint paint = new Paint();

    public TabMapLine(Context context, float x1, float y1,float x2, float y2){
        super(context);
        paint.setColor(0xFFFF0000);
        this.startX = x1;
        this.startY = y1;
        this.stopX = x2;
        this.stopY = y2;
    }

    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        canvas.drawLine(startX, startY, stopX, stopY, paint);
    }
}

TabActivity: Training.java

public class Training extends TabActivity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_training);

    Resources res = getResources();
    TabHost tabHost = getTabHost(); 
    TabHost.TabSpec spec1;
    TabHost.TabSpec spec2;
    Intent intent1;
    Intent intent2;

    intent1 = new Intent().setClass(this, TabTimer.class);

    spec1 = tabHost.newTabSpec("tab1").setIndicator("Tren",
            res.getDrawable(R.drawable.tab_1)).setContent(intent1);
    tabHost.addTab(spec1);

    intent2 = new Intent().setClass(this, TabMap.class);
   开发者_如何学C spec2 = tabHost.newTabSpec("tab2").setIndicator("Map",
            res.getDrawable(R.drawable.tab_2)).setContent(intent2);
    tabHost.addTab(spec2);

    tabHost.setCurrentTab(0);
}

}

Log cat errors

E/AndroidRuntime( 2472):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime( 2472):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime( 2472):at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime( 2472):at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime( 2472):at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 2472):at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 2472):at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime( 2472):at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2472):at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime( 2472):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime( 2472):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime( 2472):at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 2472):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime( 2472):at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
E/AndroidRuntime( 2472):at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
E/AndroidRuntime( 2472):at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
E/AndroidRuntime( 2472):at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
E/AndroidRuntime( 2472):at android.widget.TabHost.setCurrentTab(TabHost.java:323)
E/AndroidRuntime( 2472):at android.widget.TabHost.addTab(TabHost.java:213)
E/AndroidRuntime( 2472):at org.me.piana.training.Training.onCreate(Train
ing.java:39)
E/AndroidRuntime( 2472):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 2472):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime( 2472):... 11 more
E/AndroidRuntime( 2472): Caused by: java.lang.ClassCastException: android.widget.TextView
E/AndroidRuntime( 2472):at org.me.piana.training.TabTimer.onCreate(TabTimer.java:59)
E/AndroidRuntime( 2472):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 2472):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime( 2472):        ... 20 more
W/ActivityManager(58):Force finishing activity org.me.piana/.training.Training
W/ActivityManager(   58):Force finishing activity org.me.piana/.PIaNA
W/ActivityManager(   58):Activity pause timeout for HistoryRecord{45114d10 org.me.piana/.training.Training}
D/SntpClient(   58): request time failed: java.net.SocketException: Address fami
ly not supported by protocol
W/ActivityManager(   58): Activity destroy timeout for HistoryRecord{45080400 or
g.me.piana/.PIaNA}
W/ActivityManager(   58): Activity destroy timeout for HistoryRecord{45114d10 or
g.me.piana/.training.Training}


As I can see from log output, problem is in the TabTimer onCreate() method. You are trying to cast TextView object to wrong type in the line 59 of TabTimer.java

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号