我是一个新的android编码器。在这个项目中,没有红线,但当我尝试运行我的应用程序时,“不幸的是,”应用程序“已停止”弹出屏幕。虽然右边有几条黄色和紫色的线条。 这是我的代码:
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
public class Translator extends ActionBarActivity {
public ArrayAdapter<?> fromLanguageAdapter, toLanguageAdapter, englishAdapter,
chineseAdapter, spanishAdapter;
public Spinner fromLanguageSpinner = (Spinner) findViewById(R.id.spinner);
public Spinner toLanguageSpinner = (Spinner) findViewById(R.id.spinner2);
public Spinner phrases = (Spinner) findViewById(R.id.spinner3);
public TextView answer = (TextView)findViewById(R.id.answer);
public String[][] allPhrases = new String[][]{{"Hello","thank you","you are welcome","goodbye","i love you"},
{"你好","谢谢","不用客气","再见","我爱你"},
{"hola","gracias","bienvenido","adiós","te amo"}};
public Button button = (Button) findViewById(R.id.button);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_translator);
fromLanguageAdapter = ArrayAdapter.createFromResource(this,R.array.fromLanguage,
android.R.layout.simple_spinner_item);
fromLanguageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
fromLanguageSpinner.setAdapter(fromLanguageAdapter);
fromLanguageSpinner.setOnItemSelectedListener(new languageListener());
toLanguageAdapter = ArrayAdapter.createFromResource(this,R.array.toLanguage,
android.R.layout.simple_spinner_item);
toLanguageAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
toLanguageSpinner.setAdapter(toLanguageAdapter);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
translate();
}
});
}
public class languageListener implements OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView<?> parent, View v, int pos,
long id) {
//parent.getItemAtPosition(pos);
if (pos==0)
chooseEnglish(phrases);
else if (pos==1)
chooseChinese(phrases);
else if (pos==2)
chooseSpanish(phrases);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
private void chooseEnglish(Spinner spinner){
englishAdapter = ArrayAdapter.createFromResource(this,R.array.englishPhrases,
android.R.layout.simple_spinner_item);
englishAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(englishAdapter);
}
private void chooseChinese(Spinner spinner){
chineseAdapter = ArrayAdapter.createFromResource(this,R.array.chinesePhrases,
android.R.layout.simple_spinner_item);
chineseAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(chineseAdapter);
}
private void chooseSpanish(Spinner spinner){
spanishAdapter = ArrayAdapter.createFromResource(this,R.array.spanishPhrases,
android.R.layout.simple_spinner_item);
spanishAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spanishAdapter);
}
void translate(){
int toLanguagePosition = toLanguageSpinner.getSelectedItemPosition();
int whichPhrase = phrases.getSelectedItemPosition();
answer.setText(allPhrases[toLanguagePosition][whichPhrase]);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_translator, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Translator">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="From:"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:spinnerMode="dropdown"
android:entries="@array/fromLanguage" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="To:"
android:id="@+id/textView2"
android:layout_below="@+id/spinner"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner2"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:entries="@array/toLanguage" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Phrases:"
android:id="@+id/textView5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/spinner2" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spinner3"
android:layout_below="@+id/textView5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Translate"
android:id="@+id/button"
android:clickable="true"
android:onClick="buttonOnClick"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/answer"
android:layout_below="@+id/spinner3"
android:layout_toLeftOf="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
答案 0 :(得分:2)
您的错误很可能是因为您在setContentView
调用之前尝试引用布局中的对象。尝试将其更改为:
public Spinner fromLanguageSpinner;
public Spinner toLanguageSpinner;
public Spinner phrases;
public TextView answer;
public String[][] allPhrases = new String[][]{{"Hello","thank you","you are welcome","goodbye","i love you"},
{"你好","谢谢","不用客气","再见","我爱你"},
{"hola","gracias","bienvenido","adiós","te amo"}};
public Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_translator);
fromLanguageSpinner = (Spinner) findViewById(R.id.spinner);
toLanguageSpinner = (Spinner) findViewById(R.id.spinner2);
phrases = (Spinner) findViewById(R.id.spinner3);
answer = (TextView)findViewById(R.id.answer);
button = (Button) findViewById(R.id.button);
....