2016-06-13 413 views
1

我花了一些代碼在Android上製作一個簡單的表單。看起來好像代碼的某些部分引用了某些東西。 我不知道該放什麼才能使它工作?我收到錯誤。在android studio上製作表格

Error:(18, 32) error: cannot find symbol variable activity_form 
Error:(33, 88) error: package com.chalkstreet.learnandroid.main does not exist 
Error:(48, 50) error: cannot find symbol class MainSource 
Error:(57, 41) error: cannot find symbol variable menu_main 

我不認爲我需要使用,我沒有包。

例題:

Intent sender = new Intent(Form.this, 
===> ???? com.chalkstreet.learnandroid.main.Display.class); 

這裏是我的形式:

public class Form extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_form); 
    getSupportActionBar().setHomeButtonEnabled(true); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    //Initialize buttons and Edit Texts for form 
    Button btnSubmit = (Button) findViewById(R.id.button_submit); 
    Button btnSrc = (Button) findViewById(R.id.buttonSrc); 
    final EditText name = (EditText) findViewById(R.id.editText1); 
    final EditText email = (EditText) findViewById(R.id.editText2); 
    final EditText phone = (EditText) findViewById(R.id.editText4); 

    //Listener on Submit button 
    btnSubmit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent sender = new Intent(Form.this, com.chalkstreet.learnandroid.main.Display.class); 
      Bundle b1 = new Bundle(); //Bundle to wrap all data 
      b1.putString("name", name.getText().toString()); //Adding data to bundle 
      b1.putString("email", email.getText().toString()); 
      b1.putString("phone", phone.getText().toString()); 
      sender.putExtras(b1); //putExtras method to send the bundle 
      startActivity(sender); 
      Form.this.finish(); //Finish form activity to remove it from stack 
     } 
    }); 

    //Listener on source button 
    btnSrc.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent j = new Intent(Form.this, MainSource.class); 
      startActivity(j); 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 

    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    int id = item.getItemId(); 
    if (id == android.R.id.home) { 

     Form.this.finish(); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 


} 
} 

感謝

+1

看來你還沒有創建xml文件,activity_form.xml和menu_main.xml,你也可能沒有創建MainSource.java或者如果它的內部類沒有包含父文件。 –

回答

2

創建一個名爲MainSource.java文件,即如果你還沒有這樣做的話,和RES目錄下創建的菜單文件夾名爲menu_main.xml文件並添加菜單項,請在另一個佈局文件res目錄,並將它命名activity_form.xml下佈局的文件夾,然後可以用這些ID匹配那些Form.java

更多說明性添加必要的看法,menu_main可能包含

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools"> 
    <item 
     android:id="@+id/action_settings" 
     android:orderInCategory="100" 
     android:title="@string/action_settings" 
     app:showAsAction="never" /> 
</menu> 

activity_form.xml可能類似於

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <EditText 
      android:id="@+id/editText1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="Name" /> 

     <EditText 
      android:id="@+id/editText2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="Email" /> 

     <EditText 
      android:id="@+id/editText3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="Something Else" /> 

     <EditText 
      android:id="@+id/editText4" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:hint="Phone" /> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal"> 
     <Button 
      android:id="@+id/button_submit" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

     <Button 
      android:id="@+id/buttonSrc" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 
</LinearLayout> 
    </LinearLayout> 

我不知道你想要MainSource做什麼,所以我不能多說這些。我希望這有幫助。

+0

我上傳了,因爲它很好解釋,但如果你創建一個新的活動,所有文件都會自動生成。 –

1

當您創建一個明確的意圖,你只需要提供的初始上下文和結局類。

Intent i = new Intent(FirstActivity.this, ResultActivity.class);