应用程序在按钮单击时崩溃。尝试动态创建编辑文本

时间:2014-02-16 07:23:37

标签: android button android-edittext logcat

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_class_container);

    mLinearLayout = (LinearLayout) findViewById(R.id.overall_layout);
    mEditLinearLayout = (LinearLayout) findViewById(R.id.edittext_layout);

    assignName = (EditText) findViewById(R.id.assign_edit);
    assignWeight = (EditText) findViewById(R.id.weight_edit);

    addMore = (Button) findViewById(R.id.btn_add);
    addMore.setOnClickListener(onClick());
}

private OnClickListener onClick() {
    return new OnClickListener() {

        public void onClick(View v) {
            //mLinearLayout.addView(mEditLinearLayout);
        }
    };
}

XML文件:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/overall_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/gradient_gray"
    android:orientation="vertical" >

<!-- Inserts Subject Name to Save -->

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="3dp"
    android:layout_marginTop="5dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/class_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/write_class_name"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#4169E1" />

    <EditText
        android:id="@+id/class_edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/example_name"
        android:inputType="textCapWords"
        android:singleLine="true" />
</LinearLayout>

<!-- Assignment Name and Weight -->

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="3dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/assign_textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:text="@string/write_assignment_type"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#4169E1" />

    <TextView
        android:id="@+id/weight_textview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:text="@string/write_assign_percent"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#4169E1" />
</LinearLayout>

<!-- Enter information for Assignment Name and Weight -->

<LinearLayout
    android:id="@+id/edittext_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="3dp"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/assign_edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:hint="@string/example_assign"
        android:inputType="textCapWords"
        android:singleLine="true" >
    </EditText>

    <EditText
        android:id="@+id/weight_edit"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:layout_weight="1"
        android:gravity="center_horizontal"
        android:hint="@string/example_weight"
        android:inputType="number"
        android:maxLength="2"
        android:singleLine="true" >
    </EditText>
</LinearLayout>

<Button
    android:id="@+id/btn_add"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:gravity="center_vertical|left"
    android:paddingLeft="5dp"
    android:text="@string/more_assign" />

</LinearLayout>

我有整个LinearLayout,其中我有多个布局,我想点击一个按钮动态创建编辑文本,但我遇到了这个部分的问题,应用程序一直在崩溃。我不确定应该怎么做来解决这个问题。如果我去登录猫

!ENTRY com.android.ide.eclipse.adt 4 0 2014-02-15 17:40:25.957
!MESSAGE 
!STACK 0
java.lang.NullPointerException
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.getDefaultProperties(RenderSessionImpl.java:1476)
    at com.android.layoutlib.bridge.BridgeRenderSession.getDefaultProperties(BridgeRenderSession.java:68)
    at com.android.ide.eclipse.adt.internal.editors.layout.gle2.ViewHierarchy.getDefaultProperties(ViewHierarchy.java:710)
    at com.android.ide.eclipse.adt.internal.editors.layout.properties.XmlProperty.getStringValue(XmlProperty.java:209)
    at com.android.ide.eclipse.adt.internal.editors.layout.properties.XmlProperty.getValue(XmlProperty.java:221)
    at com.android.ide.eclipse.adt.internal.editors.layout.properties.XmlPropertyEditor.getText(XmlPropertyEditor.java:116)
    at com.android.ide.eclipse.adt.internal.editors.layout.properties.XmlPropertyEditor.paint(XmlPropertyEditor.java:131)
    at org.eclipse.wb.internal.core.model.property.table.PropertyTable.drawProperty(PropertyTable.java:1309)
    at org.eclipse.wb.internal.core.model.property.table.PropertyTable.drawContent(PropertyTable.java:1151)
    at org.eclipse.wb.internal.core.model.property.table.PropertyTable.handlePaint(PropertyTable.java:1094)
    at org.eclipse.wb.internal.core.model.property.table.PropertyTable.access$200(PropertyTable.java:64)
    at org.eclipse.wb.internal.core.model.property.table.PropertyTable$3.handleEvent(PropertyTable.java:187)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4136)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1458)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1481)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1466)
    at org.eclipse.swt.widgets.Control.drawWidget(Control.java:1244)
    at org.eclipse.swt.widgets.Canvas.drawWidget(Canvas.java:167)
    at org.eclipse.swt.widgets.Widget.drawRect(Widget.java:741)
    at org.eclipse.swt.widgets.Canvas.drawRect(Canvas.java:162)
    at org.eclipse.swt.widgets.Display.windowProc(Display.java:5473)
    at org.eclipse.swt.internal.cocoa.OS.objc_msgSendSuper(Native Method)
    at org.eclipse.swt.widgets.Display.applicationNextEventMatchingMask(Display.java:4875)
    at org.eclipse.swt.widgets.Display.applicationProc(Display.java:5241)
    at org.eclipse.swt.internal.cocoa.OS.objc_msgSend(Native Method)
    at org.eclipse.swt.internal.cocoa.NSApplication.nextEventMatchingMask(NSApplication.java:94)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3615)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1053)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:942)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
    at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:588)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:543)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
    at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
    at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
    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:629)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1438)

3 个答案:

答案 0 :(得分:0)

下面的代码显示了如何将LinearLayout动态添加到“主”线性布局中     LinearLayout Master;

LayoutInflater inflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Master = (LinearLayout) findViewById(R.id.container);
        inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Button button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                inflater.inflate(R.layout.row,  Master);
            }
        });

    }
   
 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Add" />

    <LinearLayout
        android:id="@+id/container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_below="@+id/button1" >
    </LinearLayout>

    </RelativeLayout>

<!--row.xml-->

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="239dp"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    </LinearLayout>

如果您只想添加一个EditText,可以跳过使用inflater并执行:

EditText txt = new EditText(this, null);
Master.addView(txt);

答案 1 :(得分:0)

我会用稍微不同的方法做到这一点!首先我会在xml中创建edit text,然后进入onCreate方法,但会使用variableOfEditText.setVisibility(View.GONE)将visiblity设置为GONE当用户点击按钮时,在onclick方法中,我会将其显示为variableOfEditText.setVisiblity(View.VISIBLE)。确保将edittext变量声明为类变量
我想给出的另一个建议是,使用onClick获取按钮,而不是使用匿名内部类,它很干净

答案 2 :(得分:0)

我猜你在这里粘贴的logcat不完整,因为它没有在任何地方提到你的代码。发生崩溃时,在logcat及其下方查找“由...引起的”行,您可能会找到对您自己的应用程序包的引用。查看此空指针在您的活动中发生的位置会非常有帮助,因为从您粘贴到问题中的代码中看不出这一点。

1。)你注释掉的行//mLinearLayout.addView(mEditLinearLayout);,不起作用,它会导致IllegalStateException,因为mEditLinearLayout已经是mLinearLayout的一部分,所以你不能再添加它。但你似乎正在获得NPE。

如果您只是希望在启动活动时在XML中定义的EditTexts不可见,但在点击时显示它们,只需在XML中使edittext_layout“消失”,如下所示:

    <LinearLayout
        android:id="@+id/edittext_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:orientation="horizontal"
        android:visibility="gone">

然后在你的onClick方法中,使布局显示,如下所示:

    public void onClick(View v) {
        mEditLinearLayout.setVisibility(View.VISIBLE);
    }

2。)如果你真的想在每次用户点击“添加”按钮时创建一个新的EditText,那么你可能不需要在edittext_layout中开始使用任何EditTexts。只需在onClick上添加一个新的EditText,将其添加到您的mEditLinearLayout,并在以后的活动的List字段中保留对它的引用。

在一个名为edit_text.xml的单独XML文件中定义一个简单的EditText,如下所示:

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

然后:

    private ArrayList<EditText> mEditTextList;

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.new_class_container);
        mEditTextList = new ArrayList<EditText>();

        mLinearLayout = (LinearLayout) findViewById(R.id.overall_layout);
        mEditLinearLayout = (LinearLayout) findViewById(R.id.edittext_layout);

        assignName = (EditText) findViewById(R.id.assign_edit);
        assignWeight = (EditText) findViewById(R.id.weight_edit);

        addMore = (Button) findViewById(R.id.btn_add);
        addMore.setOnClickListener(onClick());
    }

    private OnClickListener onClick() {
        return new OnClickListener() {
            public void onClick(View v) {
                EditText editText = (EditText) YourActivity.this.getLayoutInflater().inflate(R.layout.edit_text, null);
                mEditTextList.add(editText);
                mEditLinearLayout.addView(editText);
            }
        };
    }

请不要忘记在onDestroy中清除列表:

    @Override
        protected void onDestroy() {
        super.onDestroy();
        mEditTextList.clear();
        mEditTextList = null;
    }