基本上,这个应用程序应该询问您的姓名,并带有保存选项,单击时会弹出一个警报对话框并询问“您确定吗?”,单击“是”时,它应该说“欢迎+任何名称” . 我的问题是该应用程序在表示欢迎之前一直关闭。我将字符串声明为 userName 并在没有任何函数的情况下运行它,它只是说“欢迎,null”。但是当我做了 userName=editText.getText().toString(); 该应用程序立即关闭。请帮助我没有想法。调用欢迎页面的页面工作正常,但welcome.java 是有问题的文件。
public class Welcome extends Activity {
String userName;
final EditText editText=(EditText)findViewById(R.id.editText);
final TextView textView=(TextView)findViewById(R.id.textView);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
final TextView textView=(TextView)findViewById(R.id.textView);
final EditText editText=(EditText)findViewById(R.id.editText);
userName=editText.getText().toString();
textView.setText(String.format("Welcome, %s.", userName));
}
}
logcat 基本上就是标题,给下面一行报错:
userName=editText.getText().toString();
PS我已经在onCreate之前和之后移动了那行代码,它仍然给出了错误
我的 MainActivity.java 是
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.view.View;
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] menu={"User Preferences","Animation","Browser","Media","Take Picture"};
setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.options, menu));
}
protected void onListItemClick(ListView l, View v, int position, long id){
switch(position){
case 0:
startActivity(new Intent(MainActivity.this,userPreferences.class));
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
}
@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_main, 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);
}
}
我的 activity_main.xml 是
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/ic_launcher_sf"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="4px"
android:layout_marginRight="10px"
android:layout_marginTop="2px">
</ImageView>
<TextView
android:id="@+id/options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@+id/travel"
android:textSize="25sp"></TextView>
我的welcome.xml 是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:textSize="30dp" />
我的 userpreferences.java
public class userPreferences extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userpreferences);
Button save=(Button)findViewById(R.id.button);
save.setOnClickListener(new View.OnClickListener() {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(userPreferences.this);
@Override
public void onClick(View v) {
alertDialog.setTitle("Alert Dialog");
alertDialog.setMessage("Are you sure you want to save?");
alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(userPreferences.this,Welcome.class));
}
});
alertDialog.setNegativeButton("No", null);
alertDialog.show();
}
});
}
}
和我的用户 preferences.xml 文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_weight="1"
android:hint="Enter Your Name"
android:textSize="20dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"
android:id="@+id/button"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true" />