【问题标题】:Eclipse - Android - simple code doesn't workEclipse - Android - 简单的代码不起作用
【发布时间】:2014-12-30 22:07:23
【问题描述】:

首先,对于(假设的)愚蠢的问题,我们深表歉意。

此代码不起作用(请参阅下面的平板电脑和 LogCat 上的错误消息)

怎么了?

我在 Eclipse ADT 上工作,并尝试简单地创建一个启动进度条的按钮。 :op

我使用便宜的中国 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): 创建

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:无法启动活动 组件信息{com.NicolasDoyen.redstarcoach/com.NicolasDoyen.redstarcoach.Coach}: java.lang.NullPointerException

12-31 01:43:22.516: E/AndroidRuntime(2947): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)

12-31 01:43:22.516: E/AndroidRuntime(2947): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

12-31 01:43:22.516: E/AndroidRuntime(2947): 在 android.app.ActivityThread.access$600(ActivityThread.java:141)

12-31 01:43:22.516: E/AndroidRuntime(2947): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)

12-31 01:43:22.516: E/AndroidRuntime(2947): 在 android.os.Handler.dispatchMessage(Handler.java:99)

12-31 01:43:22.516: E/AndroidRuntime(2947): 在 android.os.Looper.loop(Looper.java:137)

12-31 01:43:22.516: E/AndroidRuntime(2947): 在 android.app.ActivityThread.main(ActivityThread.java:5041)

12-31 01:43:22.516: E/AndroidRuntime(2947): 在 java.lang.reflect.Method.invokeNative(Native Method) 12-31 01:43:22.516:E/AndroidRuntime(2947):在 java.lang.reflect.Method.invoke(Method.java:511)

12-31 01:43:22.516: E/AndroidRuntime(2947): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)

12-31 01:43:22.516: E/AndroidRuntime(2947): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

12-31 01:43:22.516: E/AndroidRuntime(2947): 在 dalvik.system.NativeStart.main(Native Method)

12-31 01:43:22.516: E/AndroidRuntime(2947): 由: java.lang.NullPointerException

12-31 01:43:22.516: E/AndroidRuntime(2947): 在 com.NicolasDoyen.redstarcoach.Coach.onCreate(Coach.java:34)

12-31 01:43:22.516: E/AndroidRuntime(2947): 在 android.app.Activity.performCreate(Activity.java:5104)

12-31 01:43:22.516: E/AndroidRuntime(2947): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)

12-31 01:43:22.516: E/AndroidRuntime(2947): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

12-31 01:43:22.516: E/AndroidRuntime(2947): ... 11 更多

这是我非常简单的 UI 的 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() {

以下作为评论,没有错误。所以我猜错误就在那里......

【问题讨论】:

  • 定义“不起作用”。究竟会发生什么?
  • 请发布您的 logcat 跟踪
  • 哪一个是错误行?由于您跳过了一些导入,因此您在程序中的行与您发布的内容不一致
  • 你在 UI 线程中睡觉,你不应该这样做。需要使用worker来等待更新进度条。
  • 请在编辑后的正文中找到答案。并且已经感谢您的宝贵时间。

标签: android eclipse adt


【解决方案1】:

所以。我在 activity_coach.xml 而不是 fragment_coach.xml 中重新定义了我的 UI。 Aaaand,它的工作原理......不知道这个片段的东西为什么以及从哪里来。但是,无论如何,它有效......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多