如何将视图添加到我切换的另一个布局

时间:2015-02-08 15:32:57

标签: android android-relativelayout

我是android的新手。在我的应用程序中,我正在尝试将视图添加到另一个布局期望主要布局。但是我在做这个时遇到错误.AVM说“不幸的是,我的应用已停止”

这是我的主要java文件,

@SuppressLint("ResourceAsColor")
public class MainActivity extends Activity {

    android.widget.RelativeLayout.LayoutParams params;

    MediaPlayer mp = new MediaPlayer();
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RelativeLayout lay =(RelativeLayout)findViewById(R.id.myrelative);
        RelativeLayout lay_2 =(RelativeLayout)findViewById(R.layout.next);
        RelativeLayout lay_3 =(RelativeLayout)findViewById(R.layout.mainprojectscreen);
       // play Song when create
       mp.reset();
       mp.stop();

       try {

           AssetFileDescriptor afd;
           afd = getAssets().openFd("a.mp3");
           mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
           mp.prepare();
           mp.start();
       } catch (IllegalStateException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }


       ImageView background = new ImageView(this);
       background.setImageResource(R.drawable.back);
       params= new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);
       background.setLayoutParams(params);
       lay.addView(background);


       ImageView play_background = new ImageView(this);
       play_background.setImageResource(R.drawable.buttonm);
       params= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
       params.height=120;
       params.width=120;
       play_background.setLayoutParams(params);
       play_background.setX(250);
       play_background.setY(80);
       lay.addView(play_background);

       ImageView play_button = new ImageView(this);
       play_button.setImageResource(R.drawable.play);
       params= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,Layou tParams.WRAP_CONTENT);
       params.height=100;
       params.width=100;
       play_button.setLayoutParams(params);
       play_button.setX(play_background.getX()+20);
       play_button.setY(play_background.getY()+10);
       play_button.setId(1);
       play_button.setOnClickListener (onClickListener);
       lay.addView(play_button);



       ImageView head_background = new ImageView(this);
       head_background.setImageResource(R.drawable.buttonm);
       params= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
       params.height=200;
       params.width=200;
       head_background.setLayoutParams(params);
       head_background.setX(10);
       head_background.setY(30);
       lay.addView(head_background);

       TextView name= new TextView(this);
       name.setText("Capulcunun \n Hikaye \n Kitabi");
       name.setTextColor(android.R.color.black);
       name.setTextSize(5);
       params= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
       name.setLayoutParams(params);
       name.setX(0);
       name.setY(0);
       lay.addView(name);

    }


    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(final View v) {
            switch(v.getId()){
                case 1:
                   setContentView(R.layout.next);
                   createLayoutTwo();
                break;

            }
        }
    };


    public void createLayoutTwo(){
        RelativeLayout lay_2 =(RelativeLayout)findViewById(R.layout.next);
        Button a = new Button(this);
        a.setText("Start new Project");
        params= new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        a.setLayoutParams(params);
        a.setX(100);
        a.setY(46);
        lay_2.addView(a);
    }

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


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}

我正在调用createLayoutTwo();当我用setContentView(R.layout.next)改变布局时; onClickListener函数中的代码。但它不执行createLayoutTwo()中所述的操作;命令。

这是我的主要xml文件:

<RelativeLayout
 android:id="@+id/myrelative" 
 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"
 android:background="#ff8D8D8D"
 tools:context="com.example.deniz.MainActivity">

以下是我的其他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" >

</RelativeLayout>

这是我得到的错误;

java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout
at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1701)
at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:382)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:389)
at android.view.View.measure(View.java:17430)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5463)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
at android.view.View.measure(View.java:17430)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:875)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17430)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:875)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17430)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.measureView(RenderSessionImpl.java:626)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.render(RenderSessionImpl.java:526)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:334)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:332)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.RenderService.createRenderSession(RenderService.java:517)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.renderWithBridge(GraphicalEditorPart.java:1584)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.recomputeLayout(GraphicalEditorPart.java:1309)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.GraphicalEditorPart.onXmlModelChanged(GraphicalEditorPart.java:1267)
at com.android.ide.eclipse.adt.internal.editors.layout.LayoutEditorDelegate.delegateXmlModelChanged(LayoutEditorDelegate.java:402)
at com.android.ide.eclipse.adt.internal.editors.common.CommonXmlEditor.xmlModelChanged(CommonXmlEditor.java:329)
at com.android.ide.eclipse.adt.internal.editors.AndroidXmlEditor$XmlModelStateListener.modelChanged(AndroidXmlEditor.java:1653)
at org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel.fireModelChanged(AbstractStructuredModel.java:526)
at org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel.internalModelChanged(AbstractStructuredModel.java:837)
at org.eclipse.wst.sse.core.internal.model.AbstractStructuredModel.changedModel(AbstractStructuredModel.java:361)
at org.eclipse.wst.xml.core.internal.document.DOMModelImpl.changedModel(DOMModelImpl.java:171)
at org.eclipse.wst.sse.core.internal.undo.StructuredTextUndoManager.undo(StructuredTextUndoManager.java:640)
at org.eclipse.wst.sse.ui.StructuredTextViewerUndoManager.undo(StructuredTextViewerUndoManager.java:183)
at org.eclipse.jface.text.TextViewer.doOperation(TextViewer.java:3997)
at org.eclipse.jface.text.source.SourceViewer.doOperation(SourceViewer.java:1018)
at org.eclipse.jface.text.source.projection.ProjectionViewer.doOperation(ProjectionViewer.java:1501)
at org.eclipse.wst.sse.ui.internal.StructuredTextViewer.doOperation(StructuredTextViewer.java:644)
at org.eclipse.ui.texteditor.TextOperationAction$1.run(TextOperationAction.java:128)
at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:70)
at org.eclipse.ui.texteditor.TextOperationAction.run(TextOperationAction.java:126)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:519)
at com.android.ide.eclipse.adt.internal.editors.layout.gle2.DelegatingAction.runWithEvent(DelegatingAction.java:136)
at com.android.ide.eclipse.adt.internal.lint.LintEditAction.runWithEvent(LintEditAction.java:46)
at org.eclipse.jface.commands.ActionHandler.execute(ActionHandler.java:122)
at org.eclipse.ui.internal.handlers.E4HandlerProxy.execute(E4HandlerProxy.java:90)
at sun.reflect.GeneratedMethodAccessor39.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.eclipse.e4.core.internal.di.MethodRequestor.execute(MethodRequestor.java:55)
at org.eclipse.e4.core.internal.di.InjectorImpl.invokeUsingClass(InjectorImpl.java:247)
at org.eclipse.e4.core.internal.di.InjectorImpl.invoke(InjectorImpl.java:229)
at org.eclipse.e4.core.contexts.ContextInjectionFactory.invoke(ContextInjectionFactory.java:132)
at org.eclipse.e4.core.commands.internal.HandlerServiceHandler.execute(HandlerServiceHandler.java:149)
at org.eclipse.core.commands.Command.executeWithChecks(Command.java:499)
at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:508)
at org.eclipse.e4.core.commands.internal.HandlerServiceImpl.executeHandler(HandlerServiceImpl.java:210)
at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.executeCommand(KeyBindingDispatcher.java:286)
at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.press(KeyBindingDispatcher.java:507)
at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.processKeyEvent(KeyBindingDispatcher.java:558)
at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.filterKeySequenceBindings(KeyBindingDispatcher.java:378)
at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher.access$0(KeyBindingDispatcher.java:324)
at org.eclipse.e4.ui.bindings.keys.KeyBindingDispatcher$KeyDownFilter.handleEvent(KeyBindingDispatcher.java:86)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.filterEvent(Display.java:1087)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4184)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1467)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1490)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1475)
at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1504)
at org.eclipse.swt.widgets.Widget.sendKeyEvent(Widget.java:1500)
at org.eclipse.swt.widgets.Canvas.sendKeyEvent(Canvas.java:496)
at org.eclipse.swt.widgets.Control.doCommandBySelector(Control.java:1060)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:5719)
at org.eclipse.swt.internal.cocoa.OS.objc_msgSend(Native Method)
at org.eclipse.swt.internal.cocoa.NSResponder.interpretKeyEvents(NSResponder.java:68)
at org.eclipse.swt.widgets.Composite.keyDown(Composite.java:594)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:5629)
at org.eclipse.swt.internal.cocoa.OS.objc_msgSendSuper(Native Method)
at org.eclipse.swt.widgets.Widget.callSuper(Widget.java:221)
at org.eclipse.swt.widgets.Widget.windowSendEvent(Widget.java:2105)
at org.eclipse.swt.widgets.Shell.windowSendEvent(Shell.java:2329)
at org.eclipse.swt.widgets.Display.windowProc(Display.java:5691)
at org.eclipse.swt.internal.cocoa.OS.objc_msgSendSuper(Native Method)
at org.eclipse.swt.widgets.Display.applicationSendEvent(Display.java:5128)
at org.eclipse.swt.widgets.Display.applicationProc(Display.java:5277)
at org.eclipse.swt.internal.cocoa.OS.objc_msgSend(Native Method)
at org.eclipse.swt.internal.cocoa.NSApplication.sendEvent(NSApplication.java:128)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3655)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1151)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1032)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:148)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:636)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:579)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:150)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:135)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:380)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:235)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:648)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:603)
at org.eclipse.equinox.launcher.Main.run(Main.java:1465)

1 个答案:

答案 0 :(得分:0)

  

java.lang.IllegalStateException:循环依赖项不能存在   RelativeLayout的

这意味着,在某些时候,您正在依赖关系图中创建一个循环。你做不了类似的事情:

  • 查看A - &gt;布局规则:以上B.
  • 查看B - &gt;布局规则:在A。下面。

这样的事情会在布局约束依赖项中创建一个循环。