【问题标题】:NullPointerException: Attempt to invoke virtual method ... on a null object reference [duplicate]NullPointerException:尝试在空对象引用上调用虚拟方法...[重复]
【发布时间】:2016-12-09 21:49:17
【问题描述】:

当我启动我的 SettingsActivity 时,我收到以下错误,但我不知道为什么,因为我一如既往地完成了所有操作。

错误:

FATAL EXCEPTION: main Process: com.android.niklasvlach.vertretung, PID: 9175 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.niklasvlach.vertretung/com.android.niklasvlach.vertretung.SettingsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) at android.app.ActivityThread.access$900(ActivityThread.java:154) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5294) 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:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference at com.android.niklasvlach.vertretung.SettingsActivity.onCreate(SettingsActivity.java:28) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)  at android.app.ActivityThread.access$900(ActivityThread.java:154)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5294)  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:904)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 

代码:

public class SettingsActivity extends AppCompatActivity {

Spinner select;
EditText username;
EditText password;

@Override
protected void onCreate(Bundle savedInstanceState) {

    select = (Spinner) findViewById(R.id.selectclass);
    username = (EditText) findViewById(R.id.username_view);
    password = (EditText) findViewById(R.id.password_view);

    select.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long text) {
            Toast.makeText(SettingsActivity.this,"" + text,Toast.LENGTH_LONG).show();

        }

        public void onNothingSelected(AdapterView<?> arg0) {

        }
    });

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
}

}

如果还有什么需要的,我会发上来的!

【问题讨论】:

    标签: java android nullpointerexception


    【解决方案1】:

    您需要在设置完 Activity 的布局后进行 UI 工作。

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings);
    
        select = (Spinner) findViewById(R.id.selectclass);
        username = (EditText) findViewById(R.id.username_view);
        password = (EditText) findViewById(R.id.password_view);
    
        select.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    
            public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long text) {
                Toast.makeText(SettingsActivity.this,"" + text,Toast.LENGTH_LONG).show();
    
            }
    
            public void onNothingSelected(AdapterView<?> arg0) {
    
            }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-05
      • 2016-07-21
      • 2016-03-13
      • 2015-03-09
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2015-01-24
      相关资源
      最近更新 更多