Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/203.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 背景圈菜单问题_Java_Android_Eclipse_Menu - Fatal编程技术网

Java 背景圈菜单问题

Java 背景圈菜单问题,java,android,eclipse,menu,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 a

很抱歉再次打扰您,我正在尝试为我制作的圆形菜单设置背景,但是应用程序崩溃了,我已经尝试了好几次,但我还没有找到答案。你能帮我吗

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));
                             }
                   }
         }

} 

logcat告诉您错误在哪一行?错误是什么?为什么它会崩溃?请在“pruebafondo.setBackgroundResource(R.drawable.circle);”中发布logcat.tha应用程序崩溃,其中我肯定会在super.oncreate之后调用setContentView。你认为我在设置背景时出错了吗?我尝试了“setContentView(R.layout.activity_test_menu);”但我没有显示背景
<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>
    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