2014-03-06 59 views
0

我在做一個登記畫面在我的應用程序中的第一頁,在MainActivity.java我把:登記屏幕的Android

public class MainActivity extends Activity { 

     private EditText username=null; 
     private EditText password=null; 
     private TextView attempts; 
     private Button login; 
     int counter = 3; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      username = (EditText)findViewById(R.id.editText1); 
      password = (EditText)findViewById(R.id.editText2); 
      attempts = (TextView)findViewById(R.id.textView3); 
      attempts.setText(Integer.toString(counter)); 
      login = (Button)findViewById(R.id.button1); 
     } 

     public void login(View view){ 
      if(username.getText().toString().equals("admin") && 
      password.getText().toString().equals("admin")){ 
      Toast.makeText(getApplicationContext(), "Redirecting...", 
      Toast.LENGTH_SHORT).show(); 
     }  
     else{ 
      Toast.makeText(getApplicationContext(), "Wrong Credentials", 
      Toast.LENGTH_SHORT).show(); 
      attempts.setBackgroundColor(Color.RED); 
      counter--; 
      attempts.setText(Integer.toString(counter)); 
      if(counter==0){ 
      login.setEnabled(false); 
      } 

     }} 

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

} 

在XML頁面我把:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="30dp" 
     android:layout_marginTop="92dp" 
     android:text="username" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView1" 
     android:layout_below="@+id/textView1" 
     android:layout_marginTop="36dp" 
     android:text="password" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/textView1" 
     android:layout_alignBottom="@+id/textView1" 
     android:layout_marginLeft="30dp" 
     android:layout_toRightOf="@+id/textView1" 
     android:ems="10" > 

     <requestFocus /> 
    </EditText> 

    <EditText 
     android:id="@+id/editText2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/textView2" 
     android:layout_alignLeft="@+id/editText1" 
     android:ems="10" 
     android:inputType="textPassword" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/editText2" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="72dp" 
     android:text="attempts" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/textView3" 
     android:layout_below="@+id/textView3" 
     android:layout_marginTop="42dp" 
     android:text="login" /> 

</RelativeLayout> 

寫作時,我得到的錯誤是在Java代碼:

username = (EditText)findViewById(R.id.editText1); 

editText1不能得到解決或不一個領域。 相同editText2,textView3和button1

請任何幫助嗎?

+0

導入正確的R文件 – CQM

+0

我做了導入,如何導入正確的R文件? – Angel

+0

我的代碼沒有收到任何錯誤。但是你沒有爲你的button1調用onClick()方法。嘗試清理和建立項目。 –

回答

0

我有自己的解決方案,希望對你有所幫助。

Project->Clean我選擇「Clean projects selected below」,選擇我的項目並選中「Build only selected projects」選中「立即開始構建」。

然後轉到Window->Preferences->General->Keys,搜索「Build clean」並輸入我自己的綁定命令。例如,按Ctrl +d

然後,當我按下Ctrl鍵+d和輸入我有乾淨和建設。

0

首先,save文件(佈局),並嘗試clean and build the project

然後: 聲明更改爲:

private EditText username; 
     private EditText password; 

而且改變升ogin method的名字,因爲你有一個名爲loginbuttonloginFunction例如,

public void loginFunction(View view) 

代答此方法按鈕loginsetOnclickListener關聯:

login.setOnclickListener(loginFunction) 
+0

這些建議不是他的錯誤我想的解決方案..... –

+0

錯誤消失但運行時它給我這個問題: Dex加載器]無法執行dex:多個dex轉換爲Dalvik格式.. 這是什麼意思? – Angel

1

當分配就像你佈局文件@+id/editText1的ID,你的IDE應該通常需要生成R.java的價值觀爲你的照顧。如果代碼生成步驟沒有發生或者由於某種原因失敗了,那麼R.id.editText1將不可用,並且您將得到您描述的錯誤。

您正在使用哪種IDE?你能清理和重建項目嗎?

你看看R.java看看你的領域是否在那裏?對於任何佈局文件中的每個ID,你應該看到在那裏產生一個恆定的,就像這樣:

public final class R { 
    (...) 

    public static final class id { 
    public static final int editText1 = ...; 
    } 

    (...) 
} 
+0

你在用什麼IDE?蝕? Android Studio?這些事情要麼自動發生(這可能是他們不適合你),或者當你點擊「Build project」(或類似的東西)時。如果它不起作用,請在此之前嘗試「清潔項目」。通常這不應該是必要的。 – hendrikbeck

0

首先清潔和重建項目。如果沒有運氣,然後嘗試以下。

有時這是發生在xml佈局文件時,如果缺少結束標籤或類似的東西。只需再檢查一下你的xml以確保你沒有忘記一些東西。另外 檢查<?xml version="1.0" encoding="utf-8"?>位於佈局文件的頂部。