Android PopupWindow中ArrayList的SetText NullPointerException。安卓

Android PopupWindow中ArrayList的SetText NullPointerException。安卓,android,arraylist,android-popupwindow,Android,Arraylist,Android Popupwindow,我想在文本视图中为弹出窗口显示一个数组。仅随机从数组中选择1项。在这种情况下,我在我的main活动中使用一个ArrayList并调用randomArray()来显示。 它在屏幕_popup.xml上不起作用。但它在Main_activity.xml中确实起作用。我正在使用TextView textView1作为主要活动&TextView txtViewArrayContent作为弹出窗口。我认为TextView已正确初始化,但与setContentView有关?任何指点都很好,谢谢 日志cat中

我想在文本视图中为弹出窗口显示一个数组。仅随机从数组中选择1项。在这种情况下,我在我的
main活动中使用一个ArrayList
并调用
randomArray()
来显示。 它在屏幕_popup.xml上不起作用。但它在Main_activity.xml中确实起作用。我正在使用
TextView textView1
作为主要活动&
TextView txtViewArrayContent
作为弹出窗口。我认为TextView已正确初始化,但与
setContentView
有关?任何指点都很好,谢谢

日志cat中的错误为:

07-17 09:25:34.655  25779-25779/com.example.testarray_02 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.testarray_02, PID: 25779
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testarray_02/com.example.testarray_02.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
主要活动包括:

public class MainActivity extends Activity {

Button btnClosePopup;
Button btnCreatePopup;
TextView newArray;
String item;

//***** Random Generator & ArrayList *****
final Random randomGenerator = new Random();
final ArrayList sample = new ArrayList() {{ add("Random Facts about Stuff"); add("Random Facts about Stuff 2"); add("Random Facts about Stuff 3"); add("Random Facts about Stuff 4");}};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //Works - main activity layout
   // newArray = (TextView)findViewById(R.id.textView1);

    // Does NOT work. ... popup layout
    newArray = (TextView)findViewById(R.id.txtViewArrayContent);

    randomArray();
    btnCreatePopup = (Button) findViewById(R.id.button1);
    btnCreatePopup.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            customPopupWindow(v);
        }
    });
}

private void randomArray() {
    //setContentView(R.layout.screen_popup);
    item = (String) sample.get(randomGenerator.nextInt(sample.size()));
    newArray.setText(item);  // Error logcat points to this line
                             // Null Exception Error
}
private PopupWindow popupWin;
private void customPopupWindow(View v){
    try {
        LayoutInflater inflater = (LayoutInflater) MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.screen_popup,
                (ViewGroup) findViewById(R.id.popup_element));
        popupWin = new PopupWindow(layout, 600, 600, true);
        popupWin.showAtLocation(layout, Gravity.CENTER, 0, 0);
        btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
        btnClosePopup.setOnClickListener(cancel_button_click_listener);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
private View.OnClickListener cancel_button_click_listener = new View.OnClickListener() {
    public void onClick(View v) {
        popupWin.dismiss();
    }
};
}
XML屏幕弹出窗口的片段

    <TextView
    android:id="@+id/txtViewArrayContent"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="5sp"
    android:text="" />

您试图通过在
activity\u main.xml
文件中按id查找来初始化
newArray
,但它不在那里,因此您会得到
NullPointerException
。要修复代码,请从
onCreate()
方法中删除
newArray
初始化和调用
randomArray()
,并将其添加到
customPopupWindwo(视图v)
中,如下所示:

private void customPopupWindow(View v) {
    LayoutInflater inflater = (LayoutInflater) MainActivity.this
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.screen_popup,
            (ViewGroup) findViewById(R.id.popup_element));
    newArray = (TextView) layout.findViewById(R.id.txtViewArrayContent);
    //omitted rest for brevity
}

它不存在/在单击按钮之前未充气