suyan94
尘世不足
2016-07-24 07:45
采纳率: 100%
浏览 4.7k
已采纳

请问为什么我只要在android studio中加入线程就会程序就会闪退呀?

下面是MainActivity的代码。

 package wjsd.sfy.suyan.test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {
    private ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar);
//    private TextView textView = (TextView)findViewById(R.id.textView);
    private Button button = (Button)findViewById(R.id.button);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button.setOnClickListener(new ButtonListener());

    }

    class ButtonListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            MyThread myThread = new MyThread();
            myThread.start();
        }
    }
    class MyThread extends Thread{
        @Override
        public void run() {
            for(int i = 0;i < 100;i++){
                try {
                    Thread.sleep(100);
                }catch (Exception e){
                    e.printStackTrace();
                }
                progressBar.setProgress(progressBar.getProgress() + 1);
            }

        }

    }
}

下面是错误信息:
07-24 07:41:57.002 4672-4672/wjsd.sfy.suyan.test W/System: ClassLoader referenced unknown path: /data/app/wjsd.sfy.suyan.test-1/lib/x86_64
07-24 07:41:57.066 4672-4672/wjsd.sfy.suyan.test D/AndroidRuntime: Shutting down VM
07-24 07:41:57.066 4672-4672/wjsd.sfy.suyan.test E/AndroidRuntime: FATAL EXCEPTION: main
Process: wjsd.sfy.suyan.test, PID: 4672
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{wjsd.sfy.suyan.test/wjsd.sfy.suyan.test.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2327)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$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$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.(AppCompatDelegateImplBase.java:68)
at android.support.v7.app.AppCompatDelegateImplV7.(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatDelegateImplV11.(AppCompatDelegateImplV11.java:28)
at android.support.v7.app.AppCompatDelegateImplV14.(AppCompatDelegateImplV14.java:41)
at android.support.v7.app.AppCompatDelegateImplV23.(AppCompatDelegateImplV23.java:29)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:188)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:172)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:512)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:184)
at wjsd.sfy.suyan.test.MainActivity.(MainActivity.java:10)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1067)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2317)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$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$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
07-24 07:42:00.272 4672-4672/wjsd.sfy.suyan.test I/Process: Sending signal. PID: 4672 SIG: 9
求教,谢谢了。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • zjh_1993
    Littlechoc 2016-07-24 08:11
    已采纳

    补充,上述第二点可能不会报错,具体见http://blog.csdn.net/androidzhaoxiaogang/article/details/8136222但还是要注意不要在非UI线程刷新UI

    点赞 1 评论
  • speedinghzl
    speedinghzl 2016-07-24 08:07

    有很明确的报错信息:NullPointerException
    我已经很久没写android代码了,可能有些地方说得不准确。
    1. 你在实例化类的时候去获取progressbar和buttom的实例引用,但是此时整个view都还没有实例化。你应该在oncreate函数里的setcontentview函数之后使用findviewbyid函数去获取button的引用。这样可以解决nullpointer的错误。
    2. 对view的操作要在主线程(UI线程)里来做(官网文档中有提到)。你可以使用hander+thread的方式等来解决,你可以自己google搜索。
    最后,我觉得你应该多看一些别人的代码,模仿着写。

    点赞 评论
  • zjh_1993
    Littlechoc 2016-07-24 08:08

    首先,button = findViewById()你得写在setContentView的后面。

    其次,等你放到后面之后还是会报错,因为不能再非UI线程对UI进行操作。也就是setProgress你不能放在MyThread中。

    点赞 评论

相关推荐