[英]TextView unreachable code
Here the code: 这里的代码:
package com.example.appbsp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
TextView textView1 = (TextView)
findViewById(R.id.textView1);
}
}
First I added a new TextView in activity_main.xml and gave it the Id: @+id/textView1 Then I entered this code in MainActivity.java for futher outputs: 首先,我在activity_main.xml中添加了一个新的TextView并为其指定了ID:@ + id / textView1,然后在MainActivity.java中输入以下代码以获取更多输出:
TextView textView1 = (TextView)
findViewById(R.id.textView1);
Then I imported android.widget.TextView but then the upper code becomes red underlined and it says unreachable code and just "remove" as quick fix. 然后,我导入了android.widget.TextView,但是随后的上部代码变成了红色下划线,并且表示无法访问的代码,只是“删除”了快速修复方法。 One month before it worked and now I don´t work anymore.
它工作之前的一个月,现在我不再工作了。 Anyone knows an answer?
有人知道答案吗?
(Target SDK:API 18; Compile with API 19) (目标SDK:API 18;使用API 19编译)
I´ma newbe so please try to give a not too complicated answer. 我是newbe,所以请尝试给出一个不太复杂的答案。 Sorry for bad English.
对不起,英语不好。 Thanks
谢谢
put return to at bottom. 放回底部。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
TextView textView1 = (TextView)
findViewById(R.id.textView1);
return true;
}
Because you wrote the return true
before that statement so that why this line TextView textView1 = (TextView) findViewById(R.id.textView1);
因为您在该语句之前写了
return true
,所以为什么这一行TextView textView1 = (TextView) findViewById(R.id.textView1);
is unreachable because compiler wont reach to this statement. 是无法访问的,因为编译器不会到达此语句。
Do like this 像这样
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
and Intialize the views in the onCreate
like 并在
onCreate
初始化视图
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView1 = (TextView) findViewById(R.id.textView1);
// you can set the text here any
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.