在调用intent时但在调用intent的super.onCreate(Bundle)
之前,我遇到了从活动到活动的运行时错误。我记得有类似的问题,但我不记得解决方法是如何解决的。以下是两项活动:
package com.erikstagg.tempo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class register extends Activity {
private EditText txtFname;
private EditText txtLname;
private EditText txtEmail;
private EditText txtPhone;
private Button btnNext;
private TextView lblWarning;
public static String Fname;
public static String Lname;
public static String email;
public static String phone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
txtFname = (EditText)findViewById((R.id.txtFname));
txtLname = (EditText)findViewById((R.id.txtLname));
txtEmail = (EditText)findViewById((R.id.txtEmail));
txtPhone = (EditText)findViewById((R.id.txtPhone));
lblWarning = (TextView)findViewById(R.id.lblWarning);
txtFname.addTextChangedListener(watcher);
txtLname.addTextChangedListener(watcher);
txtEmail.addTextChangedListener(watcher);
txtPhone.addTextChangedListener(watcher);
btnNext = (Button)findViewById(R.id.btnNext);
btnNext.setEnabled(false);
txtFname.setText(Fname);
txtLname.setText(Lname);
txtEmail.setText(email);
txtPhone.setText(phone);
}
@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_register, 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);
}
private final TextWatcher watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{ }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{}
@Override
public void afterTextChanged(Editable s) {
if (txtFname.getText().toString().trim().length() == 0 ||
txtLname.getText().toString().trim().length() == 0 ||
txtEmail.getText().toString().trim().length() == 0 ||
txtPhone.getText().toString().trim().length() == 0) {
btnNext.setEnabled(false);
lblWarning.setVisibility(View.VISIBLE);
} else {
btnNext.setEnabled(true);
lblWarning.setVisibility(View.INVISIBLE);
}
}
};
public void btnClicked(View v) {
Fname = txtFname.getText().toString();
Lname = txtLname.getText().toString();
email = txtEmail.getText().toString();
phone = txtPhone.getText().toString();
switch(v.getId()){
case R.id.btnBack:
Intent intentBack = new Intent(this, login.class);
startActivity(intentBack);
break;
case R.id.btnNext:
Intent intentNext = new Intent(this, register2.class);
startActivity(intentNext);
break;
default:
break;
}
}
}
此活动称之为:
package com.erikstagg.tempo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import java.util.Arrays;
public class register2 extends Activity implements AdapterView.OnItemSelectedListener {
private Spinner listBMonth;
private ArrayAdapter<CharSequence> bMonthAdapter = ArrayAdapter.createFromResource(this, R.array.months, android.R.layout.simple_spinner_item);
private Spinner listBDay;
private ArrayAdapter<CharSequence> bDayAdapter = ArrayAdapter.createFromResource(this, R.array.days31, android.R.layout.simple_spinner_item);
private Spinner listBYear;
private ArrayAdapter<CharSequence> bYearAdapter = ArrayAdapter.createFromResource(this, R.array.years, android.R.layout.simple_spinner_item);
private Spinner listCountry;
private ArrayAdapter<CharSequence> countryAdapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
private Spinner listState;
private ArrayAdapter<CharSequence> stateAdapter = ArrayAdapter.createFromResource(this, R.array.states, android.R.layout.simple_spinner_item);
private EditText txtAddress;
private EditText txtCity;
private EditText txtZip;
private Button btnNext;
private TextView lblWarning;
private int bMonthPos = -1;
private int bDayPos = -1;
private int bYearPos = -1;
private int countryPos = -1;
private int statePos = -1;
public static String bDate;
public static String country;
public static String state;
public static String address;
public static String city;
public static String zip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register2);
listBMonth = (Spinner)findViewById((R.id.listMonth));
listBDay = (Spinner)findViewById((R.id.listDay));
listBYear = (Spinner)findViewById((R.id.listYear));
listCountry = (Spinner)findViewById((R.id.listCountry));
listState = (Spinner)findViewById((R.id.listState));
txtCity = (EditText)findViewById((R.id.txtCity));
txtZip = (EditText)findViewById((R.id.txtZip));
txtAddress = (EditText)findViewById((R.id.txtAddress));
lblWarning = (TextView)findViewById(R.id.lblWarning);
txtAddress.addTextChangedListener(watcher);
txtCity.addTextChangedListener(watcher);
txtZip.addTextChangedListener(watcher);
btnNext = (Button)findViewById(R.id.btnNext);
btnNext.setEnabled(false);
bMonthAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
listBMonth.setAdapter(bMonthAdapter);
bDayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
listBDay.setAdapter(bDayAdapter);
bYearAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
listBYear.setAdapter(bYearAdapter);
countryAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
listCountry.setAdapter(countryAdapter);
stateAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
listState.setAdapter(stateAdapter);
listBMonth.setOnItemSelectedListener(this);
listBDay.setOnItemSelectedListener(this);
listBYear.setOnItemSelectedListener(this);
listCountry.setOnItemSelectedListener(this);
listState.setOnItemSelectedListener(this);
if (bMonthPos != -1){
listBMonth.setSelection(bMonthPos);
}
if (bDayPos != -1){
listBDay.setSelection(bDayPos);
}
if (bYearPos != -1){
listBYear.setSelection(bYearPos);
}
if (countryPos != -1){
listBYear.setSelection(bYearPos);
}
if (statePos != -1){
listBYear.setSelection(bYearPos);
}
txtAddress.setText(address);
txtCity.setText(city);
txtZip.setText(zip);
}
@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_register2, 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);
}
private final TextWatcher watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{ }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{}
@Override
public void afterTextChanged(Editable s) {
checkIfFormCompleted();
}
};
private void checkIfFormCompleted(){
if (bMonthPos == -1 ||
bDayPos == -1 ||
bYearPos == -1 ||
countryPos == -1 ||
statePos == -1 ||
txtAddress.getText().toString().trim().length() == 0 ||
txtCity.getText().toString().trim().length() == 0 ||
txtZip.getText().toString().trim().length() == 0) {
btnNext.setEnabled(false);
lblWarning.setVisibility(View.VISIBLE);
} else {
btnNext.setEnabled(true);
lblWarning.setVisibility(View.INVISIBLE);
}
}
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
checkIfFormCompleted();
bMonthPos = listBMonth.getSelectedItemPosition();
bDayPos = listBDay.getSelectedItemPosition();
bYearPos = listBYear.getSelectedItemPosition();
countryPos = listCountry.getSelectedItemPosition();
statePos = listState.getSelectedItemPosition();
int[] months31 = {0, 2, 4, 6 , 7,9 , 11};
int[] months30 = {3, 5, 8, 10};
int[] months29 = {1};
if (Arrays.asList(months31).contains(bMonthPos)){
bDayAdapter.clear();
bDayAdapter.addAll(getResources().getStringArray(R.array.days31));
listBDay.setAdapter(bDayAdapter);
}else if (Arrays.asList(months30).contains(bMonthPos)) {
bDayAdapter.clear();
bDayAdapter.addAll(getResources().getStringArray(R.array.days30));
listBDay.setAdapter(bDayAdapter);
}else if (Arrays.asList(months29).contains(bMonthPos)) {
bDayAdapter.clear();
bDayAdapter.addAll(getResources().getStringArray(R.array.days29));
listBDay.setAdapter(bDayAdapter);
}
}
// required function for "implements AdapterView.OnItemSelectedListener"
public void onNothingSelected(AdapterView<?> parent) {
// do nothing
}
public void btnClicked(View v) {
bDate = listBMonth.getSelectedItem() + " - "
+ listBMonth.getSelectedItem() + " - "
+ listBMonth.getSelectedItem();
country = getResources().getStringArray(R.array.countries)[countryPos];
state = getResources().getStringArray(R.array.states)[statePos];
address = txtAddress.getText().toString();
city = txtCity.getText().toString();
zip = txtZip.getText().toString();
switch(v.getId()){
case R.id.btnBack:
Intent intentBack = new Intent(this, register.class);
startActivity(intentBack);
break;
case R.id.btnNext:
Intent intentNext = new Intent(this, register3.class);
startActivity(intentNext);
break;
default:
break;
}
}
}
继承logcat-&gt;错误:
04-30 10:09:52.421 4742-4742/com.erikstagg.tempo E/Zygote﹕ MountEmulatedStorage() 04-30 10:09:52.421 4742-4742/com.erikstagg.tempo E/Zygote﹕ v2 04-30 10:09:52.421 4742-4742/com.erikstagg.tempo E/SELinux﹕ [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL 04-30 10:12:38.981 4742-4742/com.erikstagg.tempo E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.erikstagg.tempo, PID: 4742
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.erikstagg.tempo/com.erikstagg.tempo.register2}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2641)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2855)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:90)
at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:74)
at android.widget.ArrayAdapter.createFromResource(ArrayAdapter.java:430)
at com.erikstagg.tempo.register2.<init>(register2.java:24)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.Class.newInstance(Class.java:1650)
at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2631)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2855)
at android.app.ActivityThread.access$900(ActivityThread.java:181)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1474)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6117)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
register.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=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="false"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Account"
android:id="@+id/register1textView"
android:layout_gravity="center_horizontal"
android:paddingBottom="50dp" />
<EditText
android:layout_width="230dp"
android:layout_height="wrap_content"
android:id="@+id/txtFname"
android:layout_gravity="center_horizontal"
android:hint="First Name"
android:gravity="center"/>
<EditText
android:layout_width="230dp"
android:layout_height="wrap_content"
android:id="@+id/txtLname"
android:layout_gravity="center_horizontal"
android:hint="Last Name"
android:gravity="center" />
<EditText
android:layout_width="230dp"
android:layout_height="wrap_content"
android:id="@+id/txtEmail"
android:layout_gravity="center_horizontal"
android:hint="Email"
android:gravity="center" />
<EditText
android:layout_width="230dp"
android:layout_height="wrap_content"
android:id="@+id/txtPhone"
android:layout_gravity="center_horizontal"
android:hint="Phone Number"
android:gravity="center" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please fill out all fields!"
android:id="@+id/lblWarning"
android:gravity="center"
android:enabled="false"
android:visibility="invisible" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight=".5"
android:gravity="bottom"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="@+id/btnBack"
android:onClick="btnClicked"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="@+id/btnNext"
android:onClick="btnClicked"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
register2.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=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentStart="true"
android:layout_weight="1"
android:id="@+id/linearLayout"
android:gravity="top|bottom">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Profile Details"
android:id="@+id/register1textView"
android:layout_gravity="center_horizontal"
android:paddingBottom="50dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Birthdate"
android:id="@+id/textView2" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listMonth"
android:layout_weight="1"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listDay"
android:layout_weight="1"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listYear"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Country"
android:id="@+id/lblCountry" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listCountry"
android:layout_weight="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="State"
android:id="@+id/lblState" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listState"
android:layout_weight="1"/>
</LinearLayout>
<EditText
android:layout_width="230dp"
android:layout_height="wrap_content"
android:id="@+id/txtAddress"
android:layout_gravity="center_horizontal"
android:hint="Address"
android:gravity="center" />
<EditText
android:layout_width="230dp"
android:layout_height="wrap_content"
android:id="@+id/txtCity"
android:layout_gravity="center_horizontal"
android:hint="City"
android:gravity="center" />
<EditText
android:layout_width="230dp"
android:layout_height="wrap_content"
android:id="@+id/txtZip"
android:layout_gravity="center_horizontal"
android:hint="Zip/Postal"
android:gravity="center" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Please fill out all fields!"
android:id="@+id/lblWarning"
android:gravity="center"
android:enabled="false"
android:visibility="invisible" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight=".5"
android:gravity="bottom"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="@+id/btnBack"
android:onClick="btnClicked"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:id="@+id/btnNext"
android:onClick="btnClicked"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:0)
这是因为你的第二个Activity
中的这些行:
private ArrayAdapter<CharSequence> bMonthAdapter = ArrayAdapter.createFromResource(this, R.array.months, android.R.layout.simple_spinner_item);
您应该将createFromResources()
调用移到字段声明之外。在调用Activity
的{{1}}生命周期之前,尚未设置/创建对onCreate()
对象的内部引用。