简体   繁体   English

布局中的Android视图

[英]Android Views within Layouts

Ok so I'm running into problems creating an app (force closing) and I think it has to do with the way I implemented the layout. 好的,所以我遇到了创建应用程序(强制关闭)的问题,我认为这与我实现布局的方式有关。 So a few questions: First, I have a relative layout that includes a text input with a button next to it, a list view (still within the relative layout) and another button below that. 所以有几个问题:首先,我有一个相对布局,包括一个文本输入,旁边有一个按钮,一个列表视图(仍然在相对布局内)和另一个按钮。 This is my main xml file: 这是我的主要xml文件:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<EditText
    android:id="@+id/edit_choice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBaseline="@+id/Button1"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/Button1"
    android:hint="@string/edit_choice" />

<Button
    android:id="@+id/Button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:onClick="addString"
    android:text="@string/button_add" />

<Button
    android:id="@+id/Button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="22dp"
    android:text="@string/button_random" />


<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/Button2"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/Button1"
    tools:listitem="@android:layout/simple_list_item_1" >
</ListView>

</RelativeLayout>

Before I even changed anything in the .java files when I tried running this, only the text input and the 2 buttons appeared and the theme changed from Holo to Holo light. 在我尝试运行此文件时,我甚至更改了.java文件中的任何内容之前,只显示了文本输入和2个按钮,主题从Holo变为Holo灯。 So I'm wondering if this works, I've only seen examples where the list view matches the parent layout completely. 所以我想知道这是否有效,我只看到了列表视图完全匹配父布局的示例。

My second question is how do I handle using the input to add values to the list view, can I do that in the main activity class or can I have another class to handle the list view and still reference the main layout. 我的第二个问题是如何处理使用输入向列表视图添加值,我可以在主活动类中执行此操作,还是可以使用另一个类来处理列表视图并仍然引用主布局。

This is my Main class: 这是我的主要课程:

public class MainActivity extends Activity 
{

public ArrayList <String> choices = new ArrayList <String>();
public ListView listView = (ListView) findViewById(R.id.listView1);
public String [] choicesArray;


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

    ListView listView = (ListView) findViewById(R.id.listView1);
    choicesArray = new String [] {"You have not entered a choice yet"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, choicesArray);
    listView.setAdapter(adapter);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) 
{
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

//adds the string to the list
public void addString(View view) 
{

    choicesArray = (String[]) choices.toArray();
    EditText editText = (EditText) findViewById(R.id.edit_choice);
    String message = editText.getText().toString();
    choices.add(message);

}


}

Hopefully this makes sense and thank you for any help. 希望这是有道理的,谢谢你的帮助。

  1. Define your ArrayAdapter as member for your class (not as variable in onCreate()). 将ArrayAdapter定义为类的成员(而不是onCreate()中的变量)。
  2. Set the adapter to your listView in onCreate(). 将适配器设置为onCreate()中的listView。
  3. Set the onClickListener for your button. 为您的按钮设置onClickListener。
  4. Add the text from EditText to your adapter when you click on the button. 单击按钮时,将EditText中的文本添加到适配器。
  5. Profit! 利润!

Problem is you are calling this line 问题是你在调用这一行

public ListView listView = (ListView) findViewById(R.id.listView1);

before setting content view by setContentView() method in onCreate() so you are getting NullPointerException. 在通过onCreate()中的setContentView()方法设置内容视图之前,您将获得NullPointerException。 Do not forget that before calling findViewById you have to set content view. 不要忘记,在调用findViewById之前,您必须设置内容视图。 So delete above line because you are creating function scope listView in onCreate method and your NullPointerException problem will be solved. 因此,删除上面的行是因为您正在onCreate方法中创建函数作用域listView,并且您的NullPointerException问题将得到解决。 And also change first line of addString method like this 并且还像这样更改addString方法的第一行

choicesArray = choices.toArray(choicesArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM