Android程序无法运行,麻烦大家帮看看是怎么回事。谢谢!

兰松821203 发布于 2014/10/01 13:45
阅读 1K+
收藏 0

大家好!我是刚入门的。请问以下代码哪里有问题,怎么运行不了。谢谢! package com.gsf; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.ProgressBar; public class MainActivity extends Activity implements OnClickListener { private Button btnright, btnwrong; private ProgressBar pb; int current = pb.getProgress(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 设置全屏 this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); // 显示自定义的SurfaceView视图 setContentView(R.layout.main); btnright = (Button) findViewById(R.id.btnright); btnwrong = (Button) findViewById(R.id.btnwrong); pb = (ProgressBar) findViewById(R.id.pb); btnright.setOnClickListener(this); btnwrong.setOnClickListener(this); } @Override public void onClick(View v) { if (v == btnright) { if (current < pb.getMax()) pb.setProgress(current + 10); } if (v == btnwrong) { if (current > 10) pb.setProgress(current - 10); } } }

 

有时候控制台有提示为:[2014-10-01 22:31:49 - Emulator] This application has requested the Runtime to terminate it in an unusual way.
[2014-10-01 22:31:49 - Emulator] Please contact the application's support team for more information.

有时候LogCat弹出:E    10-01   14:3637.495    566    566   com.gsf    AndriodRuntime ……11more

 

加载中
0
carlos
carlos

楼上说的很对

10-03 01:57:02.705: E/AndroidRuntime(663): Caused by: java.lang.NullPointerException 
10-03 01:57:02.705: E/AndroidRuntime(663):  at com.gsf.MainActivity.<init>(MainActivity.java:15) 


 int current = pb.getProgress(); 

问题就在这一句

 pb 是空的,所以出错了。

你的ProgressBar pb; 你的这个进度条是布局里面的吗?

如果是,就是在Oncreate里面这样获取

ProgressBar pb = (ProgressBarfindViewById(R.id.xxx);

贴个blog你看一下

http://www.cnblogs.com/salam/archive/2010/10/06/1844703.html

如果能有用,你就采纳了吧


0
carlos
carlos

不知道你这个有啥问题,有log吗,贴出来看看。

v == btnright

这种很难相等的,比较两个对象,最好用v.equals(btnright)这种方法。

carlos
carlos
这个不是导致你这个问题的原因。你要把log贴出来,才知道是啥原因
兰松821203
谢谢!我按照您的建议去改了,但还是不行。会不会是这个程序的逻辑有问题呀?再次谢谢您的回答。
0
carlos
carlos
有啥错误提示没有?
0
兰松821203
弄了半天,才把Log弄出来:10-03 01:57:02.375: D/AndroidRuntime(663): Shutting down VM
10-03 01:57:02.455: W/dalvikvm(663): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
10-03 01:57:02.705: E/AndroidRuntime(663): FATAL EXCEPTION: main
10-03 01:57:02.705: E/AndroidRuntime(663): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.gsf/com.gsf.MainActivity}: java.lang.NullPointerException
10-03 01:57:02.705: E/AndroidRuntime(663):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
10-03 01:57:02.705: E/AndroidRuntime(663):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
10-03 01:57:02.705: E/AndroidRuntime(663):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
10-03 01:57:02.705: E/AndroidRuntime(663):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
10-03 01:57:02.705: E/AndroidRuntime(663):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 01:57:02.705: E/AndroidRuntime(663):  at android.os.Looper.loop(Looper.java:137)
10-03 01:57:02.705: E/AndroidRuntime(663):  at android.app.ActivityThread.main(ActivityThread.java:4340)
10-03 01:57:02.705: E/AndroidRuntime(663):  at java.lang.reflect.Method.invokeNative(Native Method)
10-03 01:57:02.705: E/AndroidRuntime(663):  at java.lang.reflect.Method.invoke(Method.java:511)
10-03 01:57:02.705: E/AndroidRuntime(663):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-03 01:57:02.705: E/AndroidRuntime(663):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-03 01:57:02.705: E/AndroidRuntime(663):  at dalvik.system.NativeStart.main(Native Method)
10-03 01:57:02.705: E/AndroidRuntime(663): Caused by: java.lang.NullPointerException
10-03 01:57:02.705: E/AndroidRuntime(663):  at com.gsf.MainActivity.<init>(MainActivity.java:15)
10-03 01:57:02.705: E/AndroidRuntime(663):  at java.lang.Class.newInstanceImpl(Native Method)
10-03 01:57:02.705: E/AndroidRuntime(663):  at java.lang.Class.newInstance(Class.java:1319)
10-03 01:57:02.705: E/AndroidRuntime(663):  at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
10-03 01:57:02.705: E/AndroidRuntime(663):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
10-03 01:57:02.705: E/AndroidRuntime(663):  ... 11 more
麻烦再帮看看。
0
Kinegratii
Kinegratii

空指针错误啊

java.lang.NullPointerException 
10-03 01:57:02.705: E/AndroidRuntime(663):  at com.gsf.MainActivity.<init>(MainActivity.java:15) 

这里的代码是什么?

0
兰松821203

谢谢carlog等的回答。我把代码改成如下,问题就解决了。

 

package com.gsf;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ProgressBar;

public class MainActivity extends Activity implements OnClickListener {
 private Button btnright, btnwrong;
 private ProgressBar pb;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  // 设置全屏
  this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
    WindowManager.LayoutParams.FLAG_FULLSCREEN);
  requestWindowFeature(Window.FEATURE_NO_TITLE);
  // 显示自定义的SurfaceView视图
  setContentView(R.layout.main);
  btnright = (Button) findViewById(R.id.btnright);
  btnwrong = (Button) findViewById(R.id.btnwrong);
  pb = (ProgressBar) findViewById(R.id.pb);   
  btnright.setOnClickListener(this);
  btnwrong.setOnClickListener(this);
  }
 
 @Override
 public void onClick(View v) {
  if (v == btnright) {
   pb.incrementProgressBy(10); 
  }
  
  if (v == btnwrong) {
   pb.incrementProgressBy(-10); 

  }
 }
}

 

 

返回顶部
顶部