简体   繁体   English

.setText()使我的Android应用程序崩溃

[英].setText() crashes my android app

I have tried many other questions on this site today but none of them have been helpful, i seem to have identical syntax that works for other people but every time i hit send (when i want my text fields to update) my app just crashes, I have tried a bunch of different syntax for the setText() method but nothing has worked. 我今天在此网站上尝试了许多其他问题,但没有一个对您有帮助,我似乎拥有适用于其他人的相同语法,但是每次我点击send(当我希望更新我的文本字段时),我的应用都会崩溃,我为setText()方法尝试了一堆不同的语法,但没有任何效果。

Thanks for any help in advance 感谢您的任何帮助

this is my main class 这是我的主班

package com.example.myapp;

import android.app.Activity;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class TestprojectsActivity extends Activity {

    private int index;
    private int QsClicked = 0;
    private ArrayList<String> categories = new ArrayList<String>();
    private ArrayList<Button> buttons = new ArrayList<Button>();
    Button b1;
    Button b2;
    Button b3;
    Button b4;
    Button changer;
    private int Qselected;
    TextView txt;
    EditText edit;
    private ArrayList<String> questions = new ArrayList<String>();
    private int qnum = 0;

    // updates all the buttons so we can see more categories
    public void updateButtons(View v){
        for(int i = 0; i < 4; i++){
              if(index >= categories.size()){
                  index = 0;
              }
              buttons.get(i).setText(categories.get(index));
              index++;
          }
    }

    public void Q1(View v){
        setContentView(R.layout.sub);
        Qselected = index;
        //send(null);
    }

    public void Q2(View v){
        setContentView(R.layout.sub);
        Qselected = (index+1);
        //send(null);
    }

    public void Q3(View v){
        setContentView(R.layout.sub);
        Qselected = (index+2);
        //send(null);
    }

    public void Q4(View v){
        setContentView(R.layout.sub);
        Qselected = (index+3);
        //send(null);
    }

    public void send(View v){
        edit.setText("WHY ISNT THIS WORKING?");
        if(qnum < questions.size()){
            txt.setText("i work!");
            qnum++;
        }
        else{
            qnum = 0;
            System.exit(0);
        }
    }


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    index = 0;
    b1 = (Button) findViewById(R.id.B1);
    buttons.add(b1);
    b2 = (Button) findViewById(R.id.B2);
    buttons.add(b2);
    b3 = (Button) findViewById(R.id.B3);
    buttons.add(b3);
    b4 = (Button) findViewById(R.id.B4);
    buttons.add(b4);
    txt = (TextView) findViewById(R.id.textView1);
    edit = (EditText) findViewById(R.id.editText2);
    categories.add("dumb questions");
    categories.add("smart questions");
    questions.add("hi there, what is your name?");
    questions.add("what did you have for breakfast today?");
    questions.add("how old are you?");
    questions.add("what color is your hair?");
    updateButtons(null);
}
}

this is my main xml 这是我的主要xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layoutrowtop"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
    android:id="@+id/B4"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="B4"
    android:onClick="Q4" />
<Button
    android:id="@+id/B3"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="B3"
    android:onClick="Q3" />
<Button
    android:id="@+id/B2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="B2"
    android:onClick="Q2" />
<Button
    android:id="@+id/B1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="B1"
    android:onClick="Q1" />
<Button
    android:id="@+id/Button05"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:text="More Categories"
    android:onClick="updateButtons" />

</LinearLayout>

this is my sub xml (i use it so i can have another layout) 这是我的子xml(我用它,所以我可以有另一个布局)

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




    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.22"
        android:text="Question"
         />


    <EditText
        android:id="@+id/editText2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.51"
        android:ems="10"
        android:inputType="text" />


    <Button
        android:id="@+id/send"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.08"
        android:text="Send" 
        android:onClick="send" />

    </LinearLayout>

In the activity's onCreate method you set the content view to R.layout.main and you search for the TextView that you want to set the text. 在活动的onCreate方法中,将内容视图设置为R.layout.main然后搜索要设置文本的TextView Because the TextView is not in that layout(is in the R.layout.sub and not in the R.layout.main ) it will be null and you'll get a NullPointerException when you set the text, although you set the content view as the new layout that contains that TextView (you already search for it in the onCreate method so the reference is initialized with null). 因为TextView不在该布局中(位于R.layout.sub ,而不位于R.layout.main ),所以它将为null并且在设置文本时会得到NullPointerException ,尽管您设置了内容视图作为包含该TextView的新布局(您已经在onCreate方法中搜索了它,因此引用以null初始化)。

The solution is to search for the TextView after you set the new content view( R.layout.sub ), for example: 解决方案是设置新的内容视图( R.layout.sub之后搜索TextView ,例如:

public void send(View v){
        txt = (TextView) findViewById(R.id.textView1);
        edit = (EditText) findViewById(R.id.editText2);
        edit.setText("WHY ISNT THIS WORKING?");
        if(qnum < questions.size()){
            txt.setText("i work!");
            qnum++;
        }
        else{
            qnum = 0;
            System.exit(0);
        }
    }

Ans the same thing goes for that Edittext . 答同样的事情,于是就选择了Edittext

Your activity is using main.xml as its layout, and that does not have a TextView with id editText2 . 您的活动使用main.xml作为其布局,并且没有ID为editText2的TextView。 Just putting such a view in sub.xml doesn't create the view; 仅将这样的视图放在sub.xml中并不会创建该视图。 the layout file needs to be inflated and set as the content view for the activity. 需要放大布局文件并将其设置为活动的内容视图。 Call findViewById after you change layouts and all should be well. 更改布局后调用findViewById ,一切都应该很好。

AS in when Oncreate get call main.xml was you layout xml and it has not R.id.editText2 so at that time edit would be null.......... AS在Oncreate get调用main.xml时是您布局xml,并且它没有R.id.editText2,因此在那时,编辑将为null ..........

Put this line 把这行

edit = (EditText) findViewById(R.id.editText2);

in

 public void Q1(View v){
        setContentView(R.layout.sub);
        Qselected = index;
 edit = (EditText) findViewById(R.id.editText2); <-----------------here in Q1 Q2 Q3 Q4
        //send(null);
    }

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

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