Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java TextView不可访问代码_Java_Android_Unreachable Code - Fatal编程技术网

Java TextView不可访问代码

Java TextView不可访问代码,java,android,unreachable-code,Java,Android,Unreachable 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 savedInstanceS

代码如下:

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);
}

}
首先,我在activity_main.xml中添加了一个新的TextView,并将其命名为:@+Id/textView1 然后,我在MainActivity.java中输入了以下代码以获得进一步的输出:

TextView textView1 = (TextView)
             findViewById(R.id.textView1);
然后我导入了android.widget.TextView,但是上面的代码变成了红色下划线,上面写着无法访问的代码,只是“删除”作为快速修复。一个月前我就开始工作了,现在我不再工作了。有人知道答案吗

(目标SDK:API 18;使用API 19编译)


我是新手,所以请尽量给出一个不太复杂的答案。对不起,英语不好。谢谢

,因为您在该语句之前编写了
return true
,因此这行
TextView TextView 1=(TextView)查找dviewbyid(R.id.TextView 1)无法访问,因为编译器无法访问此语句

你喜欢这样吗

@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;
}
并初始化
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
}

因为您在该语句之前编写了
return true
,所以为什么这一行
TextView textView1=(TextView)findViewById(R.id.textView1)无法访问,因为编译器无法访问此语句

你喜欢这样吗

@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;
}
并初始化
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
}

把返回到底部

@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;
}

把返回到底部

@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;
}

简单答案的Thx;)简单答案的Thx;)