首先,抱歉(假设的)愚蠢的问题。
此代码不起作用(请参阅下面的平板电脑和LogCat上的错误消息)
怎么了?
我在Eclipse ADT上工作并尝试创建一个启动进度条的按钮。 :运算
我使用便宜的中文Dalvik平板电脑来运行应用程序。
import ...
public class Coach extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.v("comment","create");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_coach);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
Button start = (Button)findViewById(R.id.start);
final ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar2);
Log.v("comment","onClickListener");
start.setOnClickListener(new View.OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
Log.v("comment","onClick");
int jumpTime = 0;
int totalProgressTime = 100;
while(jumpTime < totalProgressTime){
try {
Thread.sleep(200);
jumpTime += 5;
progressBar.incrementProgressBy(jumpTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
我在平板电脑上显示此消息“应用”......“已停止”。
LogCat:
12-31 01:43:22.206:E / Trace(2947):错误打开跟踪文件:没有这样的 文件或目录(2)
12-31 01:43:22.336:V / comment(2947):create
12-31 01:43:22.506:V / comment(2947):onClickListener
12-31 01:43:22.506:D / AndroidRuntime(2947):关闭虚拟机
12-31 01:43:22.506:W / dalvikvm(2947):threadid = 1:线程退出 未捕获的异常(组= 0x416f2930)
12-31 01:43:22.516:E / AndroidRuntime(2947):致命异常:主
12-31 01:43:22.516:E / AndroidRuntime(2947): java.lang.RuntimeException:无法启动活动 ComponentInfo {com.NicolasDoyen.redstarcoach / com.NicolasDoyen.redstarcoach.Coach}: 显示java.lang.NullPointerException
12-31 01:43:22.516:E / AndroidRuntime(2947):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-31 01:43:22.516:E / AndroidRuntime(2947):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-31 01:43:22.516:E / AndroidRuntime(2947):at android.app.ActivityThread.access $ 600(ActivityThread.java:141)
12-31 01:43:22.516:E / AndroidRuntime(2947):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1234)
12-31 01:43:22.516:E / AndroidRuntime(2947):at android.os.Handler.dispatchMessage(Handler.java:99)
12-31 01:43:22.516:E / AndroidRuntime(2947):at android.os.Looper.loop(Looper.java:137)
12-31 01:43:22.516:E / AndroidRuntime(2947):at android.app.ActivityThread.main(ActivityThread.java:5041)
12-31 01:43:22.516:E / AndroidRuntime(2947):at java.lang.reflect.Method.invokeNative(Native Method)12-31 01:43:22.516:E / AndroidRuntime(2947):at java.lang.reflect.Method.invoke(Method.java:511)
12-31 01:43:22.516:E / AndroidRuntime(2947):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
12-31 01:43:22.516:E / AndroidRuntime(2947):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-31 01:43:22.516:E / AndroidRuntime(2947):at dalvik.system.NativeStart.main(原生方法)
12-31 01:43:22.516:E / AndroidRuntime(2947):引起: 显示java.lang.NullPointerException
12-31 01:43:22.516:E / AndroidRuntime(2947):at com.NicolasDoyen.redstarcoach.Coach.onCreate(Coach.java:34)
12-31 01:43:22.516:E / AndroidRuntime(2947):at android.app.Activity.performCreate(Activity.java:5104)
12-31 01:43:22.516:E / AndroidRuntime(2947):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-31 01:43:22.516:E / AndroidRuntime(2947):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-31 01:43:22.516:E / AndroidRuntime(2947):... 11更多
以下是我非常简单的用户界面的xml定义:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.NicolasDoyen.redstarcoach.Coach$PlaceholderFragment" >
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/progressBar2"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/progressBar2"
android:text="start" />
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="20dp"
android:layout_below="@+id/start"
android:layout_centerHorizontal="true"
android:layout_marginTop="62dp" />
</RelativeLayout>
如果我转行
start.setOnClickListener(new View.OnClickListener(){
及以下作为评论,没有错误。所以我猜错误就在那里......
答案 0 :(得分:0)
因此。我在activity_coach.xml中重新定义了我的UI而不是fragment_coach.xml。 Aaaand,它有效...不知道为什么以及这些碎片来自何处。但是,无论如何,它有效......