2014-03-03 33 views
0

我真的很新的android和java,但即時通訊嘗試製作一個android應用程序。在Eclipse中無法訪問的代碼Android

我試圖做一些事情,你可以只輸入你的名字,然後它應該通過按下按鈕來查看它。 Eclipse給我「無法訪問的代碼」錯誤。我想知道你們是否可以看到我做錯了什麼。這個錯誤在兩個規則中給出,最後在它前面。如果我刪除這些規則中的一個,它只會將錯誤移至其他規則。

謝謝你在前進,
馬立克

package com.tutorial.helloworld2; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
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; 


final EditText nameField = (EditText) findViewById(R.id.nameField); 
final TextView nameView = (TextView) findViewById(R.id.nameView); 

Button button = (Button) findViewById(R.id.button); 

button.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
     nameView.setText("Hello " + nameField.getText()); 
    } 
}); 

} 

} 
+4

如果你'返回true;',爲什麼你會在這之後的任何代碼?該方法已經返回。 –

+1

非常感謝,抱歉這樣的愚蠢問題。
我是新來的,並試圖從互聯網上學習代碼 – Marekkk

+0

好吧,如果想學習這個http://www.vogella.com/tutorials/Android/article.html將是一個偉大的起點... –

回答

0

移動return true到方法,該方法執行將結束對這一說法,並沒有在下面的代碼會被執行結束。

0

是由於return聲明,return將返回true並退出當前已寫在代碼的整個方法。你應該寫這onCreate()喜歡。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    final EditText nameField = (EditText) findViewById(R.id.nameField); 
    final TextView nameView = (TextView) findViewById(R.id.nameView); 
    Button button = (Button) findViewById(R.id.button); 
    button.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      nameView.setText("Hello " + nameField.getText()); 
     } 
    }); 
} 

現在你會得到你想要的結果