背景圈菜单问题

时间:2014-01-29 14:31:18

标签: java android eclipse menu

很抱歉再次烦恼,我试图设置一个圆形菜单的背景我已经做了,但应用程序崩溃,我已经尝试了几次,但我还没弄明白。你能帮帮我吗?

TestMenuActivity.java

package com.example.circlemenu;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.Toast;

public class TestMenuActivity extends Activity {

    CircleView cView;

         @Override
         public void onCreate(Bundle savedInstanceState) {
                   super.onCreate(savedInstanceState);
                   View pruebafondo = findViewById(R.id.pruebafondo);
                   pruebafondo.setBackgroundResource(R.drawable.circle);

                   int numberOfElements = 6;
                   View[] elems = new View[numberOfElements];


                   ImageButton tv = new ImageButton(this);
                   tv.setBackgroundResource(R.drawable.images1);
                   tv.setLayoutParams(new RelativeLayout.LayoutParams(
                                      RelativeLayout.LayoutParams.WRAP_CONTENT,
                                      RelativeLayout.LayoutParams.WRAP_CONTENT));
                   elems[0] = tv;

                   tv.setOnClickListener(new OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        Toast t = Toast.makeText(getBaseContext(), "Enviar correo a un Socio",
                                Toast.LENGTH_SHORT);
                        t.show();
                    }

                   });

                   tv.setOnLongClickListener(new OnLongClickListener(){
                       public boolean onLongClick(View arg0){
                           Intent intent = new Intent(TestMenuActivity.this, prueba.class);
                           startActivity(intent);

                           return true;
                       }
                   });

                   ImageButton tv1 = new ImageButton(this);
                   tv1.setBackgroundResource(R.drawable.imagen2);
                   tv1.setLayoutParams(new RelativeLayout.LayoutParams(
                                      RelativeLayout.LayoutParams.WRAP_CONTENT,
                                      RelativeLayout.LayoutParams.WRAP_CONTENT));
                   elems[1] = tv1;

                   ImageButton tv2 = new ImageButton(this);
                   tv2.setBackgroundResource(R.drawable.image3);
                   tv2.setLayoutParams(new RelativeLayout.LayoutParams(
                                      RelativeLayout.LayoutParams.WRAP_CONTENT,
                                      RelativeLayout.LayoutParams.WRAP_CONTENT));
                   elems[2] = tv2;

                   ImageButton tv3 = new ImageButton(this);
                   tv3.setBackgroundResource(R.drawable.imagen4);
                   tv3.setLayoutParams(new RelativeLayout.LayoutParams(
                                      RelativeLayout.LayoutParams.WRAP_CONTENT,
                                      RelativeLayout.LayoutParams.WRAP_CONTENT));
                   elems[3] = tv3;

                   ImageButton tv4 = new ImageButton(this);
                   tv4.setBackgroundResource(R.drawable.imagen5);
                   tv4.setLayoutParams(new RelativeLayout.LayoutParams(
                                      RelativeLayout.LayoutParams.WRAP_CONTENT,
                                      RelativeLayout.LayoutParams.WRAP_CONTENT));
                   elems[4] = tv4;

                   ImageButton tv5 = new ImageButton(this);
                   tv5.setBackgroundResource(R.drawable.image6);
                   tv5.setLayoutParams(new RelativeLayout.LayoutParams(
                                      RelativeLayout.LayoutParams.WRAP_CONTENT,
                                      RelativeLayout.LayoutParams.WRAP_CONTENT));
                   elems[numberOfElements - 1] = tv5;

                   cView = new CircleView(this, 115, elems);
                   setContentView(cView);
         }
}

CircleView.java

package com.example.circlemenu;

import android.content.Context;
import android.view.View;
import android.widget.RelativeLayout;

public class CircleView extends RelativeLayout {
         static final int centerId = 111;
         private final int radius;

         private RelativeLayout.LayoutParams createNewRelativeLayoutParams() {
                   RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                                      RelativeLayout.LayoutParams.WRAP_CONTENT,
                                      RelativeLayout.LayoutParams.WRAP_CONTENT);
                   lp.addRule(RelativeLayout.ABOVE, centerId);
                   lp.addRule(RIGHT_OF, centerId);
                   return lp;
         }

         private View prepareElementForCircle(View elem, int distX, int distY) {
                RelativeLayout.LayoutParams lp = createNewRelativeLayoutParams();

                elem.measure(0, 0);
                int deltaX = elem.getMeasuredWidth() / 2;
                int deltaY = elem.getMeasuredHeight() / 2;
                lp.setMargins(distX - deltaX, 0, 0, radius - distY - deltaY);
                elem.setLayoutParams(lp);
                return elem;
         }

         public CircleView(Context context, int radius, View[] elements) {
                   super(context);
                   this.radius = radius;

                   RelativeLayout.LayoutParams lpView = new RelativeLayout.LayoutParams(
                                      RelativeLayout.LayoutParams.FILL_PARENT,
                                      RelativeLayout.LayoutParams.FILL_PARENT);
                   this.setLayoutParams(lpView);

                   View center = new View(context);
                   center.setId(centerId);
                   RelativeLayout.LayoutParams lpcenter = new RelativeLayout.LayoutParams(
                                      0, 0);
                   lpcenter.addRule(CENTER_HORIZONTAL);
                   lpcenter.addRule(CENTER_VERTICAL);
                   center.setLayoutParams(lpcenter);
                   this.addView(center);

                   this.addView(prepareElementForCircle(elements[0], 0, 0));
                   if (elements.length % 2 == 0) {
                            this.addView(prepareElementForCircle(elements[elements.length / 2],
                                                0, 2 * radius));
                   }
                   if (elements.length > 2) {
                             for (int i = 1; i <= (elements.length - 1) / 2; i++) {
                                      int y = i * 4 * radius / elements.length;
                                      int x = (int) Math.sqrt(Math.pow(radius, 2)
                                                         - Math.pow((radius - y), 2));
                                      this.addView(prepareElementForCircle(elements[i], x, y));
                                      this.addView(prepareElementForCircle(elements[elements.length
                                                         - i], -x, y));
                             }
                   }
         }

} 

activity_test_menu.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pruebafondo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:background="@drawable/circle"
    tools:context=".TestMenuActivity" >

</RelativeLayout>

logcat的:

    01-29 09:08:12.303: W/dalvikvm(771): threadid=1: thread exiting with uncaught exception (group=0x41465700)

       01-29 09:08:12.313: E/AndroidRuntime(771): FATAL EXCEPTION: main
     01-29 09:08:12.313: E/AndroidRuntime(771): java.lang.RuntimeException: Unable to start        activity ComponentInfo{com.example.circlemenu/com.example.circlemenu.TestMenuActivity}:  java.lang.NullPointerException
       01-29 09:08:12.313: E/AndroidRuntime(771):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
      01-29 09:08:12.313: E/AndroidRuntime(771):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
      01-29 09:08:12.313: E/AndroidRuntime(771):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-29 09:08:12.313: E/AndroidRuntime(771):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-29 09:08:12.313: E/AndroidRuntime(771):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-29 09:08:12.313: E/AndroidRuntime(771):  at android.os.Looper.loop(Looper.java:137)
01-29 09:08:12.313: E/AndroidRuntime(771):  at android.app.ActivityThread.main(ActivityThread.java:5103)
       01-29 09:08:12.313: E/AndroidRuntime(771):   at java.lang.reflect.Method.invokeNative(Native Method)
01-29 09:08:12.313: E/AndroidRuntime(771):  at java.lang.reflect.Method.invoke(Method.java:525)
01-29 09:08:12.313: E/AndroidRuntime(771):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-29 09:08:12.313: E/AndroidRuntime(771):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-29 09:08:12.313: E/AndroidRuntime(771):  at dalvik.system.NativeStart.main(Native Method)
01-29 09:08:12.313: E/AndroidRuntime(771): Caused by: java.lang.NullPointerException
01-29 09:08:12.313: E/AndroidRuntime(771):  at com.example.circlemenu.TestMenuActivity.onCreate(TestMenuActivity.java:29)
01-29 09:08:12.313: E/AndroidRuntime(771):  at android.app.Activity.performCreate(Activity.java:5133)
01-29 09:08:12.313: E/AndroidRuntime(771):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-29 09:08:12.313: E/AndroidRuntime(771):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-29 09:08:12.313: E/AndroidRuntime(771):  ... 11 more

0 个答案:

没有答案