gpt4 book ai didi

java - 致命异常 : main and Null pointer exception arise while running a simple android program

转载 作者:行者123 更新时间:2023-11-29 05:32:59 25 4
gpt4 key购买 nike

我试图在 Android 上做一些简单的事情。我创建了一个登录页面并继续工作。当我尝试创建弹出窗口时,抛出了致命异常。帮帮忙

NewProjectActivity.java

public class NewProjectActivity extends Activity {

PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
LinearLayout mainLayout;
boolean click = true;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_project);
TextView alertdetail = (TextView) findViewById(R.id.link_to_register);
alertdetail.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(mainLayout, Gravity.BOTTOM, 10, 10);
popUp.update(50, 50, 300, 80);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});


params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
tv.setText("Hi this is a sample text for popup window");
layout.addView(tv, params);
popUp.setContentView(layout);
popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10);
mainLayout.addView(alertdetail, params);
setContentView(mainLayout);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_project, menu);
return true;
}
}

最佳答案

TextView tv 未初始化,layoutpopUpmainLayout

此外,您还为同一 Activity 设置了两次 setContentView,这并没有错,但设计很糟糕

关于java - 致命异常 : main and Null pointer exception arise while running a simple android program,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20475759/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com