java.lang.NullPointerException:尝试调用虚方法' java.lang.String java.lang.Object.toString()'在null对象上(引用

时间:2016-05-21 20:00:50

标签: android android-studio

我还在学习android studio,我正在尝试创建一个能够生成时间表的应用程序(最好是学校)。我最近添加了一个活动,将在"新时间表"被选中。我在我的galaxy s5(运行android 6.0)上运行代码时得到的错误如下:

05-21 20:49:05.813 2771-2771/com.example.ben.timetablegen E/AndroidRuntime: FATAL EXCEPTION: main
                                                                        Process: com.example.ben.timetablegen, PID: 2771
                                                                        java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                                                                            at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)
                                                                            at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
                                                                            at android.widget.Spinner.measureContentWidth(Spinner.java:917)
                                                                            at android.widget.Spinner.onMeasure(Spinner.java:636)
                                                                            at android.support.v7.widget.AppCompatSpinner.onMeasure(AppCompatSpinner.java:407)
                                                                            at android.view.View.measure(View.java:20151)
                                                                            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                            at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
                                                                            at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                            at android.view.View.measure(View.java:20151)
                                                                            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                            at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
                                                                            at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                            at android.view.View.measure(View.java:20151)
                                                                            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                            at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610)
                                                                            at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:677)
                                                                            at android.view.View.measure(View.java:20151)
                                                                            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                            at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                            at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                            at android.view.View.measure(View.java:20151)
                                                                            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                            at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
                                                                            at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                            at android.view.View.measure(View.java:20151)
                                                                            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                            at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                            at android.view.View.measure(View.java:20151)
                                                                            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
                                                                            at android.widget.LinearLayout.measureVertical(LinearLayout.java:747)
                                                                            at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                            at android.view.View.measure(View.java:20151)
                                                                            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6328)
                                                                            at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                            at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3158)
                                                                            at android.view.View.measure(View.java:20151)
                                                                            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2594)
                                                                            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1549)
                                                                            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1841)
                                                                            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)
                                                                            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7397)
                                                                            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:920)
                                                                            at android.view.Choreographer.doCallbacks(Choreographer.java:695)
                                                                            at android.view.Choreographer.doFrame(Choreographer.java:631)
                                                                            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:906)
                                                                            at android.os.Handler.handleCallback(Handler.java:739)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:158)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

这两个Java活动如下: 主要活动:

package com.example.ben.timetablegen;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

public class HomeActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    String tables[] = getTableList();

// Selection of the spinner
    Spinner spinner = (Spinner) findViewById(R.id.dropdown1);

// Application of the Array to the Spinner
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,   android.R.layout.simple_spinner_item, tables);
        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // The drop down view
    spinner.setAdapter(spinnerArrayAdapter);

    int orientation = getScreenOrientation();

    if (orientation == 1){
        final TextView sessionHead = (TextView)    findViewById(R.id.sessionHead);
        final TextView monHead = (TextView) findViewById(R.id.monHead);
        final TextView tueHead = (TextView) findViewById(R.id.tueHead);
        final TextView wedHead = (TextView) findViewById(R.id.wedHead);
        final TextView thuHead = (TextView) findViewById(R.id.thuHead);
        final TextView friHead = (TextView) findViewById(R.id.friHead);

        sessionHead.setText(R.string.tableHead1Short);
        monHead.setText(R.string.tableHead2Short);
        tueHead.setText(R.string.tableHead3Short);
        wedHead.setText(R.string.tableHead4Short);
        thuHead.setText(R.string.tableHead5Short);
        friHead.setText(R.string.tableHead6Short);
    }

    if (orientation == 2){
        final TextView sessionHead = (TextView) findViewById(R.id.sessionHead);
        final TextView monHead = (TextView) findViewById(R.id.monHead);
        final TextView tueHead = (TextView) findViewById(R.id.tueHead);
        final TextView wedHead = (TextView) findViewById(R.id.wedHead);
        final TextView thuHead = (TextView) findViewById(R.id.thuHead);
        final TextView friHead = (TextView) findViewById(R.id.friHead);

        sessionHead.setText(R.string.tableHead1);
        monHead.setText(R.string.tableHead2);
        tueHead.setText(R.string.tableHead3);
        wedHead.setText(R.string.tableHead4);
        thuHead.setText(R.string.tableHead5);
        friHead.setText(R.string.tableHead6);
    }

    if (orientation == 0) {
        final TextView sessionHead = (TextView) findViewById(R.id.sessionHead);
        final TextView monHead = (TextView) findViewById(R.id.monHead);
        final TextView tueHead = (TextView) findViewById(R.id.tueHead);
        final TextView wedHead = (TextView) findViewById(R.id.wedHead);
        final TextView thuHead = (TextView) findViewById(R.id.thuHead);
        final TextView friHead = (TextView) findViewById(R.id.friHead);

        sessionHead.setText(R.string.error);
        monHead.setText(R.string.error);
        tueHead.setText(R.string.error);
        wedHead.setText(R.string.error);
        thuHead.setText(R.string.error);
        friHead.setText(R.string.error);
    }


}

public int getScreenOrientation() {

    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
        return 1;
    }
    if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE){
        return 2;
    }
    return 0;
}

public void newTimetable(){

    Intent newTimetableActivity = new Intent(this,newTimetable.class);

    newTimetableActivity.putExtra("timeTables",getTableList());

}

@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_home, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public String[] getTableList() {
    SharedPreferences prefs = getSharedPreferences("tableList",MODE_PRIVATE);
    String R[] = {"Select a table",prefs.getString("tables", null),"New table"};
    return R;
    }
}

&#34;新时间表&#34;活性:

package com.example.ben.timetablegen;

import android.app.Activity;
import android.os.Bundle;

public class newTimetable extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.new_timetable);
    }
}

我已将新时间表添加到清单中。 提前谢谢。

*我已经浏览了其他答案,因为它们从不针对我的情况,他们很少回答我的问题

0 个答案:

没有答案