-1

我为我的应用程序制作了一个计时器,但是当我尝试通过我的 listActivity 打开它时,它不断崩溃并引发异常

代码在下面,我只发布了重要的

工具.java

package com.example.taekwondobuddy.util;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class Tools extends ListActivity{

String classes[] = {"Counter","Accelermeter","Time"} ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(Tools.this,   android.R.layout.simple_list_item_1, classes));
}


@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
    String cheese = classes[position];
    try{
    Class ourclass = Class.forName("com.example.taekwondobuddy.util." + cheese);
    Intent ourIntent = new Intent(Tools.this, ourclass);
    startActivity(ourIntent);
    } catch(ClassNotFoundException e){
        e.printStackTrace();

    }
}

   }

时间.java

package com.example.taekwondobuddy.util;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Time extends Activity {

TextView mTextView;
Button mButtonStart;
Button mButtonStop;

int mCount;
Timer mTimer;

Handler mHandler = new Handler();

Runnable TheDelegatedTimerTask = new Runnable() {
    @Override
    public void run() {
        mCount++;
        mTextView.setText("Count=" + mCount);
    }
};

protected class TheTimerTask extends TimerTask {
    @Override
    public void run() {
        // What we want to say is
        // mCount++;
        // mTextView.setText("Count="+mCount);
        // But this gives
        // "Only the original thread that created a view hierarchy can touch its views."
        mHandler.post(TheDelegatedTimerTask);
    }
}

protected void timerStart() {
    if (mTimer == null) {
        mTimer = new Timer();
        mTimer.schedule(new TheTimerTask(), 0, 100); // 100 milli seconds
    }
}

protected void timerStop() {
    if (mTimer != null) {
        mTimer.cancel();
        mTimer = null;
    }
}

OnClickListener mButtonStart_OnClick = new OnClickListener() {
    @Override
    public void onClick(View v) {
        mCount = 0;
        timerStart();
    }
};

OnClickListener mButtonStop_OnClick = new OnClickListener() {
    @Override
    public void onClick(View v) {
        timerStop();
    }
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.timer);

    mTextView = (TextView) findViewById(R.id.textview);
    mButtonStart = (Button) findViewById(R.id.buttonstart);
    mButtonStop = (Button) findViewById(R.id.buttonstop);

    mButtonStart.setOnClickListener(mButtonStart_OnClick);
    mButtonStop.setOnClickListener(mButtonStop_OnClick);

}
}

计时器.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"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="26dp"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<Button
    android:id="@+id/buttonstart"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textview"
    android:layout_below="@+id/textview"
    android:layout_marginTop="50dp"
    android:text="Start" />

<Button
    android:id="@+id/buttonstop"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/buttonstart"
    android:layout_centerVertical="true"
    android:text="Stop" />

   </RelativeLayout

有什么想法有什么问题吗?

编辑

错误日志

 10-31 02:44:52.053: E/AndroidRuntime(304): FATAL EXCEPTION: main
 10-31 02:44:52.053: E/AndroidRuntime(304): android.content.ActivityNotFoundException:    Unable to find explicit activity class   {com.example.taekwondobuddy.util/com.example.taekwondobuddy.util.Time}; have you declared    this activity in your AndroidManifest.xml?
 10-31 02:44:52.053: E/AndroidRuntime(304):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1404)
 10-31 02:44:52.053: E/AndroidRuntime(304):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
 10-31 02:44:52.053: E/AndroidRuntime(304):     at android.app.Activity.startActivityForResult(Activity.java:2817)
 10-31 02:44:52.053: E/AndroidRuntime(304):     at android.app.Activity.startActivity(Activity.java:2923)
 10-31 02:44:52.053: E/AndroidRuntime(304):     at com.example.taekwondobuddy.util.Tools.onListItemClick(Tools.java:30)
10-31 02:44:52.053: E/AndroidRuntime(304):  at android.app.ListActivity$2.onItemClick(ListActivity.java:321)
10-31 02:44:52.053: E/AndroidRuntime(304):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
 10-31 02:44:52.053: E/AndroidRuntime(304):     at   android.widget.ListView.performItemClick(ListView.java:3382)
10-31 02:44:52.053: E/AndroidRuntime(304):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:1696)
10-31 02:44:52.053: E/AndroidRuntime(304):  at android.os.Handler.handleCallback(Handler.java:587)
10-31 02:44:52.053: E/AndroidRuntime(304):  at android.os.Handler.dispatchMessage(Handler.java:92)
 10-31 02:44:52.053: E/AndroidRuntime(304):     at android.os.Looper.loop(Looper.java:123)
10-31 02:44:52.053: E/AndroidRuntime(304):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-31 02:44:52.053: E/AndroidRuntime(304):  at java.lang.reflect.Method.invokeNative(Native Method)
10-31 02:44:52.053: E/AndroidRuntime(304):  at   java.lang.reflect.Method.invoke(Method.java:521)
 10-31 02:44:52.053: E/AndroidRuntime(304):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 10-31 02:44:52.053: E/AndroidRuntime(304):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 10-31 02:44:52.053: E/AndroidRuntime(304):     at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

如果没有从您的 logcat 中看到任何内容,很难知道问题出在哪里。

尝试将 TimerTask run() 更改为

mTextView.post(new TheDelegatedTimerTask());

这应该排队 UI 线程上的工作。看起来这就是您根据注释掉的代码试图弄清楚的。

于 2013-10-31T02:32:11.747 回答
0

哦,尴尬我忘了把我的清单

于 2013-10-31T03:24:25.887 回答