-1

当我尝试在底部线性布局的表格之外添加图像按钮时,我的绘图应用程序在启动时崩溃。谁能指出我正确的方向?

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.sothree.slidinguppanel.SlidingUpPanelLayout
        android:id="@+id/sliding_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clickable="true">

        <LinearLayout
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FFCCCCCC"
            android:orientation="vertical"
            tools:context=".MainActivity" >

            <!-- Top Buttons -->

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/new_btn"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:contentDescription="@string/start_new"
                    android:src="@drawable/new_pic" />

                <ImageButton
                    android:id="@+id/draw_btn"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:contentDescription="@string/brush"
                    android:src="@drawable/brush" />

                <ImageButton
                    android:id="@+id/erase_btn"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:contentDescription="@string/erase"
                    android:src="@drawable/eraser" />

                <ImageButton
                    android:id="@+id/save_btn"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:contentDescription="@string/save"
                    android:src="@drawable/save" />
            </LinearLayout>

            <!-- Custom View -->

            <com.example.drawingfun.DrawingView
                android:id="@+id/drawing"
                android:layout_width="fill_parent"
                android:layout_height="0dp"
                android:layout_marginBottom="3dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_marginTop="3dp"
                android:layout_weight="1"
                android:background="#FFFFFFFF"
                android:scaleType="centerInside" />



        </LinearLayout>

        <!-- Color Palette -->

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:orientation="vertical" 
            android:clickable="true">

            <!-- Top Row -->
            <TableLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal" >
                <TableRow>
                <LinearLayout
                    android:id="@+id/paint_colors"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >

                    <ImageButton
                                    android:layout_width="@dimen/large_brush"
                                    android:layout_height="@dimen/large_brush"
                                    android:layout_margin="4dp"
                                    android:background = "@drawable/colorbutton1"
                                    android:contentDescription="@string/paint"
                                    android:onClick="paintClicked"
                                    android:src="@drawable/colorbutton1"
                                    android:tag="#FFE21A1A" />
                                <ImageButton
                                    android:layout_width="@dimen/large_brush"
                                    android:layout_height="@dimen/large_brush"
                                    android:layout_margin="4dp"
                                    android:background="@drawable/colorbutton2"
                                    android:contentDescription="@string/paint"
                                    android:onClick="paintClicked"
                                    android:src="@drawable/colorbutton2"
                                    android:tag="#FFF2D820" />
                                <ImageButton
                                    android:layout_width="@dimen/large_brush"
                                    android:layout_height="@dimen/large_brush"
                                    android:layout_margin="4dp"
                                    android:background="@drawable/colorbutton3"
                                    android:contentDescription="@string/paint"
                                    android:onClick="paintClicked"
                                    android:src="@drawable/colorbutton3"
                                    android:tag="#FF35EF22" />
                                <ImageButton
                                    android:layout_width="@dimen/large_brush"
                                    android:layout_height="@dimen/large_brush"
                                    android:layout_margin="4dp"
                                    android:background="@drawable/colorbutton4"
                                    android:contentDescription="@string/paint"
                                    android:onClick="paintClicked"
                                    android:src="@drawable/colorbutton4"
                                    android:tag="#FF26D3EA" />
                                <ImageButton
                                    android:layout_width="@dimen/large_brush"
                                    android:layout_height="@dimen/large_brush"
                                    android:layout_margin="4dp"
                                    android:background="@drawable/colorbutton5"
                                    android:contentDescription="@string/paint"
                                    android:onClick="paintClicked"
                                    android:src="@drawable/colorbutton5"
                                    android:tag="#FFF73E9B" />
                                <ImageButton
                                    android:layout_width="@dimen/large_brush"
                                    android:layout_height="@dimen/large_brush"
                                    android:layout_margin="4dp"
                                    android:background="@drawable/colorbutton6"
                                    android:contentDescription="@string/paint"
                                    android:onClick="paintClicked"
                                    android:src="@drawable/colorbutton6"
                                    android:tag="#FFFFFFFF" />
                </LinearLayout>
                </TableRow>
                <!-- Bottom Row -->
                <TableRow>
                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" >

                    <ImageButton
                        android:layout_width="@dimen/large_brush"
                        android:layout_height="@dimen/large_brush"
                        android:layout_margin="4dp"
                        android:background="@drawable/colorbutton7"
                        android:contentDescription="@string/paint"
                                    android:onClick="paintClicked"
                                    android:src="@drawable/colorbutton7"
                                    android:tag="#FF7A4426"/>
                               <ImageButton
                                    android:layout_width="@dimen/large_brush"
                                    android:layout_height="@dimen/large_brush"
                                    android:layout_margin="4dp"
                                    android:background="@drawable/colorbutton8"
                                    android:contentDescription="@string/paint"
                                    android:onClick="paintClicked"
                                    android:src="@drawable/colorbutton8"
                                    android:tag="#FFF74F1C"/>
                               <ImageButton
                                    android:layout_width="@dimen/large_brush"
                                    android:layout_height="@dimen/large_brush"
                                    android:layout_margin="4dp"
                                    android:background="@drawable/colorbutton9"
                                    android:contentDescription="@string/paint"
                                    android:onClick="paintClicked"
                                    android:src="@drawable/colorbutton9"
                                    android:tag="#FF217C14" />
                               <ImageButton
                                    android:layout_width="@dimen/large_brush"
                                    android:layout_height="@dimen/large_brush"
                                    android:layout_margin="4dp"
                                    android:background="@drawable/colorbutton10"
                                    android:contentDescription="@string/paint"
                                    android:onClick="paintClicked"
                                    android:src="@drawable/colorbutton10"
                                    android:tag="#FF312EBC"/>
                               <ImageButton
                                    android:layout_width="@dimen/large_brush"
                                    android:layout_height="@dimen/large_brush"
                                    android:layout_margin="4dp"
                                    android:background="@drawable/colorbutton11"
                                    android:contentDescription="@string/paint"
                                    android:onClick="paintClicked"
                                    android:src="@drawable/colorbutton11"
                                    android:tag="#FF5D1F93" />
                               <ImageButton
                                    android:layout_width="@dimen/large_brush"
                                    android:layout_height="@dimen/large_brush"
                                    android:layout_margin="4dp"
                                    android:background="@drawable/colorbutton12"
                                    android:contentDescription="@string/paint"
                                    android:onClick="paintClicked"
                                    android:src="@drawable/colorbutton12"
                                    android:tag="#FF000000"/>
                </LinearLayout>
                </TableRow>
            </TableLayout>
                <ImageButton
                        android:id="@+id/botDrag"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="8dp"
                        android:gravity="center_horizontal"
                        android:background = "@drawable/sendbutton"
                        android:contentDescription="@string/drag"
                        android:src="@drawable/sendbutton"
                        android:tag="#FFE21A1A" />      

        </LinearLayout>
    </com.sothree.slidinguppanel.SlidingUpPanelLayout>

</RelativeLayout>

爪哇:

package com.example.drawingfun;

import java.util.UUID;

import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TableRow;
import android.widget.TableLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.sothree.slidinguppanel.SlidingUpPanelLayout;
import com.sothree.slidinguppanel.SlidingUpPanelLayout.PanelSlideListener;
import android.widget.SeekBar; 
import android.widget.SeekBar.OnSeekBarChangeListener;

/**
 * This is demo code to accompany the Mobiletuts+ tutorial series:
 * - Android SDK: Create a Drawing App
 * 
 * Sue Smith
 * August 2013
 *
 */
public class MainActivity extends Activity implements OnClickListener {

    //custom drawing view
    private DrawingView drawView;
    private View botDrag;
    //buttons
    private ImageButton currPaint, drawBtn, eraseBtn, newBtn, saveBtn;
    //sizes
    private float mediumBrush;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //get drawing view
        drawView = (DrawingView)findViewById(R.id.drawing);
        //get bottom panel drag view
        botDrag = (View)findViewById(R.id.botDrag);

        //get the palette and first color button
        LinearLayout paintLayout = (LinearLayout)findViewById(R.id.paint_colors);
        currPaint = (ImageButton)paintLayout.getChildAt(0);
        currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));

        //Send current brush size to color palette



        //sizes from dimensions
        mediumBrush = getResources().getInteger(R.integer.medium_size);

        //draw button
        drawBtn = (ImageButton)findViewById(R.id.draw_btn);
        drawBtn.setOnClickListener(this);

        //set initial size
        drawView.setBrushSize(mediumBrush);

        //erase button
        eraseBtn = (ImageButton)findViewById(R.id.erase_btn);
        eraseBtn.setOnClickListener(this);

        //new button
        newBtn = (ImageButton)findViewById(R.id.new_btn);
        newBtn.setOnClickListener(this);

        //save button
        saveBtn = (ImageButton)findViewById(R.id.save_btn);
        saveBtn.setOnClickListener(this);

        SlidingUpPanelLayout layout = (SlidingUpPanelLayout) findViewById(R.id.sliding_layout);
        layout.setShadowDrawable(getResources().getDrawable(R.drawable.above_shadow));
        layout.setAnchorPoint(0.3f);
        layout.setDragView(botDrag);
        layout.setPanelSlideListener(new PanelSlideListener() {

            @Override
            public void onPanelSlide(View panel, float slideOffset) {
                if (slideOffset < 0.2) {
                    if (getActionBar().isShowing()) {
                        getActionBar().hide();
                    }
                } else {
                    if (!getActionBar().isShowing()) {
                        getActionBar().show();
                    }
                }
            }

            @Override
            public void onPanelExpanded(View panel) {


            }

            @Override
            public void onPanelCollapsed(View panel) {


            }

            @Override
            public void onPanelAnchored(View panel) {


            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    //user clicked paint
    public void paintClicked(View view){
        //use chosen color

        //set erase false
        drawView.setErase(false);
        drawView.setBrushSize(drawView.getLastBrushSize());

        if(view!=currPaint){
            ImageButton imgView = (ImageButton)view;
            String color = view.getTag().toString();
            drawView.setColor(color);
            //update ui
            imgView.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
            currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint));
            currPaint=(ImageButton)view;
        }
    }

    @Override
    public void onClick(View view){

        if(view.getId()==R.id.draw_btn){
            //draw button clicked
            final Dialog brushDialog = new Dialog(this);
            brushDialog.setTitle("Brush size:");
            brushDialog.setContentView(R.layout.brush_seekbar);
            brushDialog.setCancelable(true);


            SeekBar brushSeekbar = (SeekBar) brushDialog.findViewById(R.id.brushSize_seekbar);
            int brushSize = brushSeekbar.getProgress();
            brushSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {


                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProgressChanged(SeekBar seekBar, int progress,
                        boolean fromUser) {
                    if (fromUser)
                    drawView.setErase(false); //turn off eraser
                    drawView.setBrushSize(progress); //Change StrokeWidth 

                }

            });

             Button closeBrushBtn = (Button) brushDialog.findViewById(R.id.closeBrushBtn);
             // if decline button is clicked, close the custom dialog
             closeBrushBtn.setOnClickListener(new OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     // Close dialog
                     brushDialog.dismiss();
                 }
             });
            brushDialog.show(); 
            //pass brush size to DrawingView

        }
        else if(view.getId()==R.id.erase_btn){
            //switch to erase - choose size
            final Dialog brushDialog = new Dialog(this);
            brushDialog.setTitle("Erase size:");
            brushDialog.setContentView(R.layout.brush_seekbar);
            brushDialog.setCancelable(true);


            SeekBar brushSeekbar = (SeekBar) brushDialog.findViewById(R.id.brushSize_seekbar);

            brushSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {


                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onProgressChanged(SeekBar seekBar, int progress,
                        boolean fromUser) {
                    if (fromUser)
                    drawView.setErase(true);
                    drawView.setBrushSize(progress); //Change StrokeWidth       
                }
            });

             Button closeBrushBtn = (Button) brushDialog.findViewById(R.id.closeBrushBtn);
             // if decline button is clicked, close the custom dialog
             closeBrushBtn.setOnClickListener(new OnClickListener() {
                 @Override
                 public void onClick(View v) {
                     // Close dialog
                     brushDialog.dismiss();
                 }
             });
             brushDialog.show();
        }
        else if(view.getId()==R.id.new_btn){
            //new button
            AlertDialog.Builder newDialog = new AlertDialog.Builder(this);
            newDialog.setTitle("New drawing");
            newDialog.setMessage("Start new drawing (you will lose the current drawing)?");
            newDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int which){
                    drawView.startNew();
                    dialog.dismiss();
                }
            });
            newDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int which){
                    dialog.cancel();
                }
            });
            newDialog.show();
        }
        else if(view.getId()==R.id.save_btn){
            //save drawing
            AlertDialog.Builder saveDialog = new AlertDialog.Builder(this);
            saveDialog.setTitle("Save drawing");
            saveDialog.setMessage("Save drawing to device Gallery?");
            saveDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int which){
                    //save drawing
                    drawView.setDrawingCacheEnabled(true);
                    //attempt to save
                    String imgSaved = MediaStore.Images.Media.insertImage(
                            getContentResolver(), drawView.getDrawingCache(),
                            UUID.randomUUID().toString()+".png", "drawing");
                    //feedback
                    if(imgSaved!=null){
                        Toast savedToast = Toast.makeText(getApplicationContext(), 
                                "Drawing saved to Gallery!", Toast.LENGTH_SHORT);
                        savedToast.show();
                    }
                    else{
                        Toast unsavedToast = Toast.makeText(getApplicationContext(), 
                                "Oops! Image could not be saved.", Toast.LENGTH_SHORT);
                        unsavedToast.show();
                    }
                    drawView.destroyDrawingCache();
                }
            });
            saveDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
                public void onClick(DialogInterface dialog, int which){
                    dialog.cancel();
                }
            });
            saveDialog.show();
        }
    }



}

日志猫:

10-26 20:55:10.716: E/AndroidRuntime(883): FATAL EXCEPTION: main
10-26 20:55:10.716: E/AndroidRuntime(883): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.drawingfun/com.example.drawingfun.MainActivity}: java.lang.ClassCastException: android.widget.TableRow cannot be cast to android.widget.ImageButton
10-26 20:55:10.716: E/AndroidRuntime(883):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-26 20:55:10.716: E/AndroidRuntime(883):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-26 20:55:10.716: E/AndroidRuntime(883):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-26 20:55:10.716: E/AndroidRuntime(883):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-26 20:55:10.716: E/AndroidRuntime(883):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-26 20:55:10.716: E/AndroidRuntime(883):  at android.os.Looper.loop(Looper.java:137)
10-26 20:55:10.716: E/AndroidRuntime(883):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-26 20:55:10.716: E/AndroidRuntime(883):  at java.lang.reflect.Method.invokeNative(Native Method)
10-26 20:55:10.716: E/AndroidRuntime(883):  at java.lang.reflect.Method.invoke(Method.java:525)
10-26 20:55:10.716: E/AndroidRuntime(883):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-26 20:55:10.716: E/AndroidRuntime(883):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-26 20:55:10.716: E/AndroidRuntime(883):  at dalvik.system.NativeStart.main(Native Method)
10-26 20:55:10.716: E/AndroidRuntime(883): Caused by: java.lang.ClassCastException: android.widget.TableRow cannot be cast to android.widget.ImageButton
10-26 20:55:10.716: E/AndroidRuntime(883):  at com.example.drawingfun.MainActivity.onCreate(MainActivity.java:53)
10-26 20:55:10.716: E/AndroidRuntime(883):  at android.app.Activity.performCreate(Activity.java:5133)
10-26 20:55:10.716: E/AndroidRuntime(883):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-26 20:55:10.716: E/AndroidRuntime(883):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-26 20:55:10.716: E/AndroidRuntime(883):  ... 11 more

感谢您的帮助,我是 Android 开发的新手,在这里迷路了。

4

1 回答 1

0

您的底部线性布局位于表格布局内。或者你指的是imagebutton botDrag?

于 2013-10-27T05:04:33.773 回答