欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

2020-09-21

程序员文章站 2022-03-11 22:37:08
有大佬帮我看一下有什么问题吗。。。运行不出来这个界面,显示app停止运行package com.example.hp.myapplication;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android....

有大佬帮我看一下有什么问题吗。。。运行不出来这个界面,显示app停止运行
package com.example.hp.myapplication;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.ArrayAdapter;import android.widget.AutoCompleteTextView;import android.widget.ListView;import android.widget.ProgressBar;import android.widget.SeekBar;import android.widget.Spinner;import android.widget.Toast;import java.util.ArrayList;import java.util.List;public class CountActivity extends AppCompatActivity { ProgressBar pb; SeekBar seek; AutoCompleteTextView act; Spinner sp; ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pb=findViewById(R.id.progressBar3); seek=findViewById(R.id.seekBar); act=findViewById(R.id.autoCompleteTextView2); sp=findViewById(R.id.spinner2); lv =findViewById(R.id.listview2); //往该控件中添加可以自动补齐的字符 //字符放在集合中 List list=new ArrayList(); list.add(“china”); list.add(“blue”); list.add(“tianjin”); list.add(“aa”); list.add(“aaa”); list.add(“aaab”); //绑定需要适配器 ArrayAdapter adapter=new ArrayAdapter(CountActivity.this,android.R.layout.simple_dropdown_item_1line,list); //绑定适配器 act.setAdapter(adapter); ArrayAdapter adapter1=new ArrayAdapter(CountActivity.this,android.R.layout.select_dialog_multichoice,list); sp.setAdapter(adapter1); lv.setAdapter(adapter); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // String item1= lv.getSelectedItem().toString(); String item2=lv.getItemAtPosition(position).toString(); Toast.makeText(CountActivity.this ,"—", Toast.LENGTH_SHORT).show(); } }); //seekBar的监听事件 seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { Toast.makeText(CountActivity.this,“当前进度为:”+progress,Toast.LENGTH_LONG).show(); } @Override public void onStartTrackingTouch(SeekBar seekBar) { Toast.makeText(CountActivity.this,“开始点击了!”,Toast.LENGTH_LONG).show(); } @Override public void onStopTrackingTouch(SeekBar seekBar) { Toast.makeText(CountActivity.this,“结束点击了!”,Toast.LENGTH_LONG).show(); } }); //开启一个线程,实现一秒钟进度增加10 new Thread(new Runnable() { @Override public void run() { int i=0; while(i<=100) { //进度变化 pb.setProgress(i); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } i=i+10; } } }).start(); }}

日志文件
Process: com.example.hp.myapplication, PID: 24843 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hp.myapplication/com.example.hp.myapplication.CountActivity}: java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.AutoCompleteTextView.setAdapter(android.widget.ListAdapter)’ on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread H . h a n d l e M e s s a g e ( A c t i v i t y T h r e a d . j a v a : 1344 ) a t a n d r o i d . o s . H a n d l e r . d i s p a t c h M e s s a g e ( H a n d l e r . j a v a : 102 ) a t a n d r o i d . o s . L o o p e r . l o o p ( L o o p e r . j a v a : 148 ) a t a n d r o i d . a p p . A c t i v i t y T h r e a d . m a i n ( A c t i v i t y T h r e a d . j a v a : 5417 ) a t j a v a . l a n g . r e f l e c t . M e t h o d . i n v o k e ( N a t i v e M e t h o d ) a t c o m . a n d r o i d . i n t e r n a l . o s . Z y g o t e I n i t H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit H.handleMessage(ActivityThread.java:1344)atandroid.os.Handler.dispatchMessage(Handler.java:102)atandroid.os.Looper.loop(Looper.java:148)atandroid.app.ActivityThread.main(ActivityThread.java:5417)atjava.lang.reflect.Method.invoke(NativeMethod)atcom.android.internal.os.ZygoteInitMethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘void android.widget.AutoCompleteTextView.setAdapter(android.widget.ListAdapter)’ on a null object reference at com.example.hp.myapplication.CountActivity.onCreate(CountActivity.java:47) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread H . h a n d l e M e s s a g e ( A c t i v i t y T h r e a d . j a v a : 1344 )   a t a n d r o i d . o s . H a n d l e r . d i s p a t c h M e s s a g e ( H a n d l e r . j a v a : 102 )   a t a n d r o i d . o s . L o o p e r . l o o p ( L o o p e r . j a v a : 148 )   a t a n d r o i d . a p p . A c t i v i t y T h r e a d . m a i n ( A c t i v i t y T h r e a d . j a v a : 5417 )   a t j a v a . l a n g . r e f l e c t . M e t h o d . i n v o k e ( N a t i v e M e t h o d )   a t c o m . a n d r o i d . i n t e r n a l . o s . Z y g o t e I n i t H.handleMessage(ActivityThread.java:1344)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit H.handleMessage(ActivityThread.java:1344) atandroid.os.Handler.dispatchMessage(Handler.java:102) atandroid.os.Looper.loop(Looper.java:148) atandroid.app.ActivityThread.main(ActivityThread.java:5417) atjava.lang.reflect.Method.invoke(NativeMethod) atcom.android.internal.os.ZygoteInitMethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 09-20 14:34:54.154 24843-24843/com.example.hp.myapplication I/Process: Sending signal. PID: 24843 SIG: 9

本文地址:https://blog.csdn.net/m0_50419564/article/details/108713112

相关标签: 笔记