当我运行该程序,然后我面对的问题,运行

时间:2019-02-02 19:23:17

标签: android

public class MainActivity extends AppCompatActivity {

Spinner basic_command;
String basic_cmd_buff[]= null;

Button button;

ArrayAdapter<String> adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    basic_command = (Spinner) findViewById(R.id.basic_command);


    basic_cmd_buff = new String[]
            {
              "WOPEN","CSQ","CREG","CGREG?","IPCONNECT"
            };

    adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,basic_cmd_buff);
    basic_command.setAdapter(adapter);
}

public void back(View view)
{
    finish();
}



}

这是错误日志:

  

02-02 13:57:26.562 27179-27179 / com.example.sunbeam.test_application E / AndroidRuntime:FATAL EXCEPTION:main                                                                                         流程:com.example.sunbeam.test_application,PID:27179                                                                                         java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.sunbeam.test_application / com.example.sunbeam.test_application.MainActivity}:java.lang.NullPointerException                                                                                             在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)                                                                                             在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)                                                                                             在android.app.ActivityThread.access $ 800(ActivityThread.java:135)                                                                                             在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)                                                                                             在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                             在android.os.Looper.loop(Looper.java:136)                                                                                             在android.app.ActivityThread.main(ActivityThread.java:5017)                                                                                             在java.lang.reflect.Method.invokeNative(本机方法)                                                                                             在java.lang.reflect.Method.invoke(Method.java:515)                                                                                             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)                                                                                             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)                                                                                             在dalvik.system.NativeStart.main(本机方法)                                                                                          造成原因:java.lang.NullPointerException                                                                                             在com.example.sunbeam.test_application.MainActivity.onCreate(MainActivity.java:38)                                                                                             在android.app.Activity.performCreate(Activity.java:5231)                                                                                             在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)                                                                                             在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)                                                                                             在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)                                                                                             在android.app.ActivityThread.access $ 800(ActivityThread.java:135)                                                                                             在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196)                                                                                             在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                             在android.os.Looper.loop(Looper.java:136)                                                                                             在android.app.ActivityThread.main(ActivityThread.java:5017)                                                                                             在java.lang.reflect.Method.invokeNative(本机方法)                                                                                             在java.lang.reflect.Method.invoke(Method.java:515)                                                                                             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779)                                                                                             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

1 个答案:

答案 0 :(得分:1)

崩溃报告告诉您该框架无法启动您的Activity,然后告诉您原因。

之后
  

原因:

在崩溃报告中,MainActivity.java中的第38行存在NullPointerException。

这可能是由这条线引起的,

basic_command.setAdapter(adapter);

其中basic_command为空,因为

findViewById(R.id.basic_command);

在找不到具有给定ID的View时返回null。请检查您的XML布局文件是正确的。