开发者

Android: View crashes when I have two EditText boxes with @+id defined in the xml

开发者 https://www.devze.com 2023-02-13 14:50 出处:网络
Another newbie question here.I\'m trying to create a form which takes several text input fields from the user, however my view keeps crashing/failing to load with the following error reported in the l

Another newbie question here. I'm trying to create a form which takes several text input fields from the user, however my view keeps crashing/failing to load with the following error reported in the log:

Window already focused, ignoring focus gain of:com.android.internal.view.IInputMethodClient$Stub$Proxy@628a9148

This is what my xml looks like (this crashes)

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
android:layout_height="wrap_content">"
<LinearLayout
    android:orientation="vertical"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_gravity="top">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Please enter contact details\n\nName:"/>
    <EditText 
        android:id="@+id/name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:maxLength="30"
        android:maxLines="1"
        android:hint="@string/compose_name"></EditText>
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="\nSurname:"/>
    <EditText
        android:id="@+id/surname"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:layout_below="@id/name"
            android:maxLength="30"
            android:maxLines="1"
            android:hint="@string/compose_surname"></EditText>-->
       <Button
            android:id="@+id/new_contact_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/submit" />

</LinearLayout>
</ScrollView>

However if I remove just the line:

android:id="@+id/surname"

It works (well at least it loads the view, of course I can't access the content in the EditText field without creating an id for it).

The view also fails if I add @+id tags to both the TextView fields (but works if I add only one).

What's going on here? I thought you should be able to label multiple view fields in your UI (all the examples in my book let me do this)?

I'm using NetBeans to develop on.

EDIT: Adding Javacode:

package org.me.savingsdepositrecord;
import android.app.Activity;
import android.os.Bundle;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;

public class NewContact extends Activity 
    implements OnClickListener, View.OnKeyListener {
//private EditText nameField;
//private String name;
private Button submitButton;
EditText nameField = null;
EditText surnameField = null;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.new_contact);

    nameField = (EditText) findViewById(R.id.name);
    nameField.setOnKeyListener(this);

    surnameField = (EditText) findViewById(R.id.surname);

    submitButton = (Button) findViewById(R.id.new_contact_button);

    // Set up click listeners for all the buttons
    submitButton.setOnClickListener(this);
}

@Override
public boolean onKey(View v, int k开发者_开发百科eyCode, KeyEvent event) {
    if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
        (keyCode == KeyEvent.KEYCODE_ENTER)) {
            String name = nameField.getText().toString();
            String surname = surnameField.getText().toString();
            // TODO: Save Nickname setting (strNicknameToSave)
            return true;
    }
    return false;
}


public void onClick(View v) {
    switch (v.getId()) {
    case R.id.new_contact_button:
        Intent i = new Intent(this, MainActivity.class);
        finish();
        startActivity(i);
        break;
        // More buttons go here (if any) ...
    }
}

}


Yes, you can label multiple fields. It might be a good to take out the android:layout_below attribute since that isn't supported in LinearLayout.

Otherwise, your Java code might be the culprit. Can you post it as well?

Edit: I also tested your code and found no errors, although I had to remove the '-->' and the @string references.

0

精彩评论

暂无评论...
验证码 换一张
取 消