问题
Basically this app is supposed to ask for your name, with an option to save, which when click, an alert dialog pops up and asks 'are you sure?', when yes is clicked, it should say 'welcome + whatever name put'. my problem is that the app keeps shutting down before it says welcome. I declared the string as userName and ran it without any function to the string, and it just said 'welcome, null'. but when i did userName=editText.getText().toString(); the app shut down immediately. Please HELP I'm out of ideas. the page which calls the welcome page works fine, but the welcome.java is the file with the issue.
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));
}
}
The logcat is basically the title, giving an error to the following line:
userName=editText.getText().toString();
PS I've moved that line of code before and after onCreate and it still gave errors
My MainActivity.java is
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);
}
}
My activity_main.xml is
<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>
My welcome.xml is
<?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" />
my 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();
}
});
}
}
and my user preferences.xml file
<?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" />
回答1:
The problem lies in both of these
final EditText editText=(EditText)findViewById(R.id.editText);
final TextView textView=(TextView)findViewById(R.id.textView);
Do not do findViewById without setContentView() called, or especially outside the methods. Make it:
private EditText editText;
private TextView textView;
And also: inside your onCreate, you don't have to redeclare the views just do:
textView=(TextView)findViewById(R.id.textView);
editText=(EditText)findViewById(R.id.editText);
Plus: Add an EditText on your welcome.xml
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:hint="THIS IS AN EDITTEXT" />
回答2:
The problem is that you have 2 different views in 2 different xml's called from one single activity. Which is why you get the nullpointerexception. so instead pass the context as an extra via an intent to the required activity and implement it in the findviewbyid method.
来源:https://stackoverflow.com/questions/32062310/attempt-to-invoke-virtual-method-android-view-view-android-view-window-findview