Java 我的Android应用程序在发布时崩溃
我正在尝试构建一个基本的应用程序来显示新闻故事(目前硬编码到Strings.xml文件中)。但它在启动时不断崩溃,即使代码中没有显示错误 这是我代码的Java部分(我还可以提供任何其他文件,甚至是我工作区的压缩副本,如果这对任何人都有帮助的话) }我想是因为Java 我的Android应用程序在发布时崩溃,java,android,Java,Android,我正在尝试构建一个基本的应用程序来显示新闻故事(目前硬编码到Strings.xml文件中)。但它在启动时不断崩溃,即使代码中没有显示错误 这是我代码的Java部分(我还可以提供任何其他文件,甚至是我工作区的压缩副本,如果这对任何人都有帮助的话) }我想是因为 final Button button = (Button) findViewById(R.id.LinearLayout1); 检查您的活动\u main.xml并传递按钮的ID,而不是线性布局1 同时将此代码移到onCreate中的下
final Button button = (Button) findViewById(R.id.LinearLayout1);
检查您的活动\u main.xml
并传递按钮的ID,而不是线性布局1
同时将此代码移到onCreate中的下面
final Button next = (Button) findViewById(R.id.LinearLayout1);
final Resources res = getResources();
final TextView textView = new TextView(null);
在setContentView之后的onCreate中移动这些
Resources res;
TextView textView;
Button next;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
res = getResources();
textView = new TextView(this); // use Activity COntext
next = (Button) findViewById(R.id.LinearLayout1);
getResources()
是上下文方法,一旦创建了活动,活动上下文就可用findViewById
在当前插入布局中查找id为的视图。您需要将布局设置为“活动”,然后初始化视图
最后
public TextView (Context context)
但是你有
TextView textView = new TextView(null);
使用此
代替null
你也有
setContentView(R.layout.activity_main);
setContentView(textView);
对于同一个活动,setContentView超过两次是一个糟糕的设计
也使用此textView=新的textView(此)代码>您有一个文本视图,但未将其添加到活动中
读
试着用那种方式做
private Button next = null;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
next = (Button) findViewById(R.id.LinearLayout1);
final Button button = (Button) findViewById(R.id.LinearLayout1);
button.setText("" + res.getString(R.string.News) + "\n\n" + res.getString(R.string.Story_Title_1) + "\n\n" + res.getString(R.string.Story_Title_2) + "\n\n" + res.getString(R.string.Story_Title_3) + "\n\n" + res.getString(R.string.Story_Title_4) + "\n\n" + res.getString(R.string.Story_Title_5));
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
OpenNews();
}
});
移动next=(按钮)findViewById(R.id.LinearLayout1)代码>在setContentView
之后创建活动,并使用textView=newtextView(MainActivity.this)代码>而不是textView=newtextview(空)代码>在setContentView
之后,logcat的错误是什么?把它贴在这里。确定R.id.LinearLayout1是一个按钮吗?
private Button next = null;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
next = (Button) findViewById(R.id.LinearLayout1);
final Button button = (Button) findViewById(R.id.LinearLayout1);
button.setText("" + res.getString(R.string.News) + "\n\n" + res.getString(R.string.Story_Title_1) + "\n\n" + res.getString(R.string.Story_Title_2) + "\n\n" + res.getString(R.string.Story_Title_3) + "\n\n" + res.getString(R.string.Story_Title_4) + "\n\n" + res.getString(R.string.Story_Title_5));
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
OpenNews();
}
});