简体   繁体   中英

Android: draw a canvas within a fragment

I want to draw multiple elements in a canvas on my Android. But I use fragments. After reading similar questions on SO ( Draw a line within a Fragment , How to draw in a fragment , ...) I have tried this code in my fragment to simple draw a rectangle, but it doesn't work:

public class ShowProfileFragment extends Fragment {


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_show_profils, container, false);

    RelativeLayout relativeLayout = (RelativeLayout) rootView.findViewById(R.id.rect);
    relativeLayout.addView(new Rectangle(getActivity()));

    return rootView;
}

private class Rectangle extends View {
    Paint paint = new Paint();

    public Rectangle(Context context) {
        super(context);
    }
    @Override
    public void onDraw(Canvas canvas) {
        paint.setColor(Color.GREEN);
        Rect rect = new Rect(20, 56, 200, 112);
        canvas.drawRect(rect, paint );
    }
}
}

I retrieve a NullPointerException because of this line: relativeLayout.addView(new Rectangle(getActivity()));

The result of getActivity() is my MainActivity.

The complete error log:

01-29 08:11:01.871    9966-9966/uie.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: uie.app, PID: 9966
java.lang.NullPointerException
        at info.uie.limux.ShowProfileFragment.onCreateView(ShowProfileFragment.java:25)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
        at android.view.View.measure(View.java:16628)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16628)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
        at android.view.View.measure(View.java:16628)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2298)
        at android.view.View.measure(View.java:16628)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5622)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:544)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:212)
        at android.app.ActivityThread.main(ActivityThread.java:5135)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
        at dalvik.system.NativeStart.main(Native Method)

Edit:

Here is the code of my fragment_show_rooms.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#fa6a6a">


<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="51dp"
    android:background="#444"
    android:id="@+id/rect">

    </RelativeLayout>
</RelativeLayout>

(Just to make this proper):

Add the rect RelativeLayout in your fragment_show_profils.xml file and not in fragment_show_rooms.xml.

:-)

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