加载我的活动时出现NullPointerException

时间:2012-05-22 15:16:32

标签: android exception layout android-activity nullpointerexception

我从屏幕'A'开始。在屏幕A中,我导航到屏幕'B',它将方向从纵向更改为横向。如果我现在按下后退按钮就会出现此异常。

它甚至没有达到OnCreate代码

E/AndroidRuntime(518): FATAL EXCEPTION: main    
E/AndroidRuntime(518): java.lang.RuntimeException: Unable to start activity ComponentInfo{be.coenen/be.coenen.TimeregistrationCardActivity}: java.lang.NullPointerException    
E/AndroidRuntime(518):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)    
E/AndroidRuntime(518):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)    
E/AndroidRuntime(518):  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3815)    
E/AndroidRuntime(518):  at android.app.ActivityThread.access$2400(ActivityThread.java:125)    
E/AndroidRuntime(518):  at android.app.ActivityThread$H.      handleMessage(ActivityThread.java:2037)    
E/AndroidRuntime(518):  at android.os.Handler.dispatchMessage(Handler.java:99)    
E/AndroidRuntime(518):  at android.os.Looper.loop(Looper.java:123)    
E/AndroidRuntime(518):  at android.app.ActivityThread.main(ActivityThread.java:4627)    
E/AndroidRuntime(518):  at java.lang.reflect.Method.invokeNative(Native Method)    
E/AndroidRuntime(518):  at java.lang.reflect.Method.invoke(Method.java:521)    
E/AndroidRuntime(518):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)    
E/AndroidRuntime(518):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)    
E/AndroidRuntime(518):  at dalvik.system.NativeStart.main(Native Method)    
E/AndroidRuntime(518): Caused by: java.lang.NullPointerException    
E/AndroidRuntime(518): at be.coenen.TimeregistrationCardActivity.onCreateDialog(TimeregistrationCardActivity.java:314)    
E/AndroidRuntime(518):  at android.app.Activity.onCreateDialog(Activity.java:2472)    
E/AndroidRuntime(518):  at android.app.Activity.createDialog(Activity.java:881)    
E/AndroidRuntime(518):  at android.app.Activity.restoreManagedDialogs(Activity.java:870)    
E/AndroidRuntime(518):  at android.app.Activity.performRestoreInstanceState(Activity.java:816)    
E/AndroidRuntime(518):  at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1096)    
E/AndroidRuntime(518):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2641)

` OnCreate屏幕A:

public class TimeregistrationOverviewActivity extends Activity {
/** Called when the activity is first created. */
private Context context;
private ArrayList<TimeregistrationOverview> listTimeRegOverView;
private TimeregistrationOverviewAdapter listViewAdapter;
private ListView listView;
private String emp, dateTimeRegistration;
private TextView txtDateDisplay;
private Button btnPickDate, btnInterventionRapport;
private int intYear, intMonth, intDay, arrayCount;
private Intent i = new Intent();
final Calendar c = Calendar.getInstance();
final int DATE_DIALOG_ID = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    try{

    super.onCreate(savedInstanceState);
    context = getApplicationContext();
    setContentView(R.layout.main);


    listView = (ListView) findViewById(R.id.android_TOlist1);
    txtDateDisplay = (TextView) findViewById(R.id.TOdateDisplay);
    btnPickDate = (Button) findViewById(R.id.TObtnDatum);
    btnPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

    intYear = c.get(Calendar.YEAR);
    intMonth = c.get(Calendar.MONTH);
    intDay = c.get(Calendar.DAY_OF_MONTH);
    // Get Employee ID
    i = getIntent();
    emp = i.getExtras().getString("Employee");
    if (!(i.getExtras().getString("Date") == null)) {
        dateTimeRegistration = i.getExtras().getString("Date");

    }

    // display the current date
    updateDisplay();

    // gegevens ophalen/updaten
    new DownloadData().execute(LogInActivity.SERVICE_URI
            + "GetTimeregistrationOverview?Date=" + dateTimeRegistration
            + "&Employee=" + emp);
} catch (Exception e) {
    ShowError();
}
}

OnCreate屏幕B:

public class TimeregistrationCardActivity extends Activity {
/** Called when the activity is first created. */
private TimeregistratrationCard timeRegistrationCard;
private Intent intent = new Intent();
private int id, minute, hour, minuteNow, hourNow;
private TextView date, customerName, description, taskDocNo, activity,
        externalInfo, internalInfo, percentageComplete, project, startTime,
        stopTime;
private CheckBox coaching;
private String employeeNo, dateTimeregistration;
private ProgressDialog m_ProgressDialog;
private Calendar c = Calendar.getInstance();

static final int TIME_DIALOG_ID = 0, TIME_DIALOG_ID2 = 1,
        DATE_DIALOG_ID = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.timeregistrationcard);

    m_ProgressDialog = ProgressDialog.show(
            TimeregistrationCardActivity.this, "", getResources()
                    .getString(R.string.ProgressDialog), true);

    date = (TextView) findViewById(R.id.TRKtxtDatum);
    customerName = (TextView) findViewById(R.id.TRKtxtKlantNaam);
    description = (TextView) findViewById(R.id.TRKtxtDescription);
    taskDocNo = (TextView) findViewById(R.id.TRKtxtTaskDocNo);
    activity = (TextView) findViewById(R.id.TRKtxtActivity);
    coaching = (CheckBox) findViewById(R.id.TRKchkCoaching);
    externalInfo = (TextView) findViewById(R.id.TRKtxtExternalInformation);
    internalInfo = (TextView) findViewById(R.id.TRKtxtInternalInformation);
    percentageComplete = (TextView) findViewById(R.id.TRKtxtPercentageComplete);
    project = (TextView) findViewById(R.id.TRKtxtProject);
    startTime = (TextView) findViewById(R.id.TRKtxtStartTime);
    stopTime = (TextView) findViewById(R.id.TRKtxtStopTime);

    intent = getIntent();
    id = intent.getExtras().getInt("ID");
    employeeNo = intent.getExtras().getString("Emp");
    dateTimeregistration = intent.getExtras().getString("Date");
    new DownloadData().execute(LogInActivity.SERVICE_URI
            + "GetTimeregistrationCard?ID=" + id);

}

2 个答案:

答案 0 :(得分:3)

查看代码中的这一行,无论返回活动时是否为null

E/AndroidRuntime(518): Caused by: java.lang.NullPointerException

E/AndroidRuntime(518): at be.coenen.TimeregistrationCardActivity.onCreateDialog(TimeregistrationCardActivity.java:314)

答案 1 :(得分:1)

使用以下代码

在您的两项活动中使用onConfigurationChanged方法
public void onConfigurationChanged(Configuration newConfig) 
{
    super.onConfigurationChanged(newConfig);

    // Checks the orientation of the screen
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
    {
        Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
    {
        Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

而不是Toast只需使用启动要打开的活动的活动。我的意思是,在屏幕A的onConfigurationChanged方法中,它从纵向改变了景观意味着,只需在那里启动屏幕A而不是Toast

不要忘记在活动的清单文件中添加以下代码 -

<activity android:name=".your Activity name"  // Your activity name.
      android:configChanges="orientation|keyboardHidden"
      android:label="@string/app_name">

希望这会对你有所帮助。