简体   繁体   中英

Android Application not displaying on device as it does in Android Studio

I'm attempting to make a program which displays the following image:

期望的应用外观

That image comes from the 'Display' window on Android Studio for the XML file for the activity I'm attempting to display. This window includes two fragments, though only one of them has visible elements right now.

However, when running the program on an android device, I get just a blank screen, with none of the visible objects or functionality that I've been programming into the fragments. I do still get the title of the activity at the top though.

What should be appearing is the two icons in the bottom (which are two image buttons from ToolbarFragment) and a blank space to be taken up by a canvas object from DrawingFragment. I am not sure how to draw the canvas yet, but at the very least the buttons should be appearing.

I assume it's something to do with the XML files or the activity itself, so I've provided the code for them below. Any help you can give will be greatly appreciated.

DrawingActivity.java

package com.example.chris.drawingtest;

import android.app.Activity;
import android.util.Log;


public class DrawingActivity extends Activity
    implements ToolbarFragment.ToolSelectionListener {

    public void sendNewValue(int newValue) {
        Log.i("Data received from Toolbar Fragment: ", "The ID of the button pressed is " + newValue);
    }
}

activity_drawing.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="bottom"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".com.example.chris.drawingtest.DrawingActivity">

    <fragment
        android:name="com.example.chris.drawingtest.DrawingFragment"
        android:id="@+id/Drawing"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout="@layout/fragment_drawing"
    />

    <fragment
        android:name="com.example.chris.drawingtest.ToolbarFragment"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Toolbar"
        tools:layout="@layout/fragment_toolbar"
    />

</LinearLayout>

EDIT: Added Fragment code.

ToolbarFragment.java

package com.example.chris.drawingtest;

import android.app.Activity;
import android.app.Fragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;

import com.example.chris.drawingtest.R;

/**
 * Created by Chris on 11/28/2014.
 */
public class ToolbarFragment extends Fragment {

    ToolSelectionListener mCallback;

    public interface ToolSelectionListener {
        public void sendNewValue(int newValue);
    }

    public void clicked(ImageButton imageButton) {
        mCallback.sendNewValue(imageButton.getId());
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_toolbar, container, false);
    }

    private ImageButton pencilButton = (ImageButton) getActivity().findViewById(R.id.pencil);
    private ImageButton eraserButton = (ImageButton) getActivity().findViewById(R.id.eraser);


}

DrawingFragment.java

package com.example.chris.drawingtest;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.example.chris.drawingtest.R;

/**
 * Created by Chris on 11/28/2014.
 */
public class DrawingFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_drawing, container, false);
    }

    private class DrawView extends View {

        private Path drawpath = new Path();
        private Paint drawpaint = new Paint();
        private Paint canvaspaint;
        private Canvas drawcanvas;
        private Bitmap canvasBitmap;

        private int paintColor = 0xFF000000;    //opaque black for pencil

        public DrawView(Context context) {
            super(context);
            drawpaint.setColor(paintColor);

            drawpaint.setStrokeWidth(10);
            drawpaint.setStyle(Paint.Style.STROKE);
            drawpaint.setStrokeJoin(Paint.Join.ROUND);
            drawpaint.setStrokeCap(Paint.Cap.ROUND);

            canvaspaint = new Paint(Paint.DITHER_FLAG);
        }

        protected void OnSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w,h,oldw,oldh);

            canvasBitmap = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
            drawcanvas = new Canvas(canvasBitmap);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            drawcanvas.drawBitmap(canvasBitmap,0,0,drawpaint);
            canvas.drawPath(drawpath, drawpaint);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float touchX = event.getX();
            float touchY = event.getY();

            switch(event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    drawpath.moveTo(touchX,touchY);
                    break;
                case MotionEvent.ACTION_MOVE:
                    drawpath.lineTo(touchX,touchY);
                    break;
                case MotionEvent.ACTION_UP:
                    drawcanvas.drawPath(drawpath, drawpaint);
                    drawpath.reset();
                    break;
                default:
                    return false;
            }
            invalidate();
            return true;
        }

    }

   Integer value = 0;

}

I might be wrong but should the activity_drawing.xml not be attached to DrawingActivity.java? It looks like activity_drawing.xml isn't linked to anything.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawing);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM