将Chronometer添加到我的Activity会产生错误

时间:2012-07-31 02:14:33

标签: android

我为游戏制作了一个活动,根据所需部分选择图像。但是当我添加天文台来控制我的活动时间时,它会出错并强制关闭活动。但是在我的代码中,当我添加计时器代码时没有错误。在我的Logcat上它说:“未捕获的处理程序:由于未捕获的异常而导致主程序退出”以及Logcat上的更多错误。这是我的代码,请帮我找到问题,提前谢谢!

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

public class Maingame extends Activity {
    private Gallery gallery;
    Chronometer mChronometer;


    @Override
    public void onCreate(Bundle savedInstanceState) {


    super.onCreate(savedInstanceState);
    setContentView(R.layout.maingame);

    Button button;
    mChronometer = (Chronometer) findViewById(R.id.time);
    // Watch for button clicks.
    button = (Button) findViewById(R.id.start);
    button.setOnClickListener(mStartListener);

    }

View.OnClickListener mStartListener = new OnClickListener() {
    public void onClick(View v) {
        mChronometer.start();
    }
};

{

    gallery = (Gallery) findViewById(R.id.examplegallery);
    gallery.setAdapter(new AddImgAdp(this));

    gallery.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView parent, View v, int position, long id) {
    // Displaying the position when the gallery item in clicked
        if (position == 0) {
            //Toast.makeText(Maingame.this, "heart", Toast.LENGTH_SHORT).show();
            TextView select = (TextView) findViewById(R.id.txt1);
              select.setText("heart"); 

        } else if (position == 1) {
            //Toast.makeText(Maingame.this, "lungs", Toast.LENGTH_SHORT).show();
            TextView select = (TextView) findViewById(R.id.txt2);
              select.setText("lungs");
        } else if (position == 2) {
            //Toast.makeText(Maingame.this, "brain", Toast.LENGTH_SHORT).show();
            TextView select = (TextView) findViewById(R.id.txt3);
              select.setText("brain");


        }
    }
    });
    Button clear = (Button)findViewById(R.id.clear);
    clear.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            ((TextView) findViewById(R.id.txt1)).setText("");
            ((TextView) findViewById(R.id.txt2)).setText("");
            ((TextView) findViewById(R.id.txt3)).setText("");
        }
    });
    Button pause = (Button)findViewById(R.id.pause);
    pause.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            moveTaskToBack(true);
        }

    });

    }


    public class AddImgAdp extends BaseAdapter {
    int GalItemBg;
    private Context cont;

    // Adding images.
    private Integer[] Imgid = {
    R.drawable.bladder, R.drawable.kidney, R.drawable.largeintestine, R.drawable.liver, R.drawable.lungs, R.drawable.stomach
    };

    public AddImgAdp(Context c) {
    cont = c;
    TypedArray typArray = obtainStyledAttributes(R.styleable.BodyPartsGame);
    GalItemBg = typArray.getResourceId(R.styleable.BodyPartsGame_android_galleryItemBackground, 0);
    typArray.recycle();
    }

    public int getCount() {
    return Imgid.length;
    }

    public Object getItem(int position) {
    return position;
    }

    public long getItemId(int position) {
    return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imgView = new ImageView(cont);

    imgView.setImageResource(Imgid[position]);
    // Fixing width & height for image to display
    imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
    imgView.setScaleType(ImageView.ScaleType.FIT_XY);
    imgView.setBackgroundResource(GalItemBg);

    return imgView;
    }
    }
    }

这是我的LogCat:

07-31 02:05:55.150: E/AndroidRuntime(224): Uncaught handler: thread main exiting due to uncaught exception
07-31 02:05:55.180: E/AndroidRuntime(224): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{drj.thesis.tridi/drj.thesis.tridi.Maingame}: java.lang.NullPointerException
07-31 02:05:55.180: E/AndroidRuntime(224):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
07-31 02:05:55.180: E/AndroidRuntime(224):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
07-31 02:05:55.180: E/AndroidRuntime(224):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
07-31 02:05:55.180: E/AndroidRuntime(224):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
07-31 02:05:55.180: E/AndroidRuntime(224):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 02:05:55.180: E/AndroidRuntime(224):  at android.os.Looper.loop(Looper.java:123)
07-31 02:05:55.180: E/AndroidRuntime(224):  at android.app.ActivityThread.main(ActivityThread.java:4363)
07-31 02:05:55.180: E/AndroidRuntime(224):  at java.lang.reflect.Method.invokeNative(Native Method)
07-31 02:05:55.180: E/AndroidRuntime(224):  at java.lang.reflect.Method.invoke(Method.java:521)
07-31 02:05:55.180: E/AndroidRuntime(224):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
07-31 02:05:55.180: E/AndroidRuntime(224):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
07-31 02:05:55.180: E/AndroidRuntime(224):  at dalvik.system.NativeStart.main(Native Method)
07-31 02:05:55.180: E/AndroidRuntime(224): Caused by: java.lang.NullPointerException
07-31 02:05:55.180: E/AndroidRuntime(224):  at android.app.Activity.findViewById(Activity.java:1612)
07-31 02:05:55.180: E/AndroidRuntime(224):  at drj.thesis.tridi.Maingame.<init>(Maingame.java:49)
07-31 02:05:55.180: E/AndroidRuntime(224):  at java.lang.Class.newInstanceImpl(Native Method)
07-31 02:05:55.180: E/AndroidRuntime(224):  at java.lang.Class.newInstance(Class.java:1479)
07-31 02:05:55.180: E/AndroidRuntime(224):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-31 02:05:55.180: E/AndroidRuntime(224):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
07-31 02:05:55.180: E/AndroidRuntime(224):  ... 11 more

这是我的maingame.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/palybg">





    <LinearLayout
        android:layout_width="70dp"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:gravity="bottom"
        android:orientation="vertical" android:layout_marginTop="10dp">

        <Chronometer
            android:id="@+id/time"
            android:layout_width="50dp"
            android:layout_height="25dp"
            android:layout_gravity="center" android:textColor="#000000" android:layout_marginTop="10dp"/>

        <Button
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Start" android:layout_gravity="center"/>

        <Button
            android:id="@+id/button1"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center" android:background="@drawable/finishbtn"/>

        <Button
            android:id="@+id/pause"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center" android:background="@drawable/pause"/>
         <Button
            android:id="@+id/clear"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="center" android:background="@drawable/clear"/>

<TextView
        android:id="@+id/txt1"
        android:background="@drawable/rack"
        android:layout_width="65dp"
        android:gravity="center"
        android:layout_height="25dp"
        android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#000000" android:textStyle="bold" android:typeface="sans"/>
<TextView
        android:id="@+id/txt2"
        android:background="@drawable/rack"
        android:layout_width="65dp"
        android:gravity="center"
        android:layout_height="25dp"
        android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#000000" android:textStyle="bold" android:typeface="sans"/>
<TextView
        android:id="@+id/txt3"
        android:background="@drawable/rack"
        android:layout_width="65dp"
        android:gravity="center"
        android:layout_height="25dp"
        android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#000000" android:textStyle="bold" android:typeface="sans"/>

    <TextView
        android:id="@+id/txt1"
        android:background="@drawable/rack"
        android:layout_width="65dp"
        android:gravity="center"
        android:layout_height="25dp"
        android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#000000" android:textStyle="bold" android:typeface="sans"/>
<TextView
        android:id="@+id/txt2"
        android:background="@drawable/rack"
        android:layout_width="65dp"
        android:gravity="center"
        android:layout_height="25dp"
        android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#000000" android:textStyle="bold" android:typeface="sans"/>
<TextView
        android:id="@+id/txt3"
        android:background="@drawable/rack"
        android:layout_width="65dp"
        android:gravity="center"
        android:layout_height="25dp"
        android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#000000" android:textStyle="bold" android:typeface="sans"/>

<TextView
        android:id="@+id/txt1"
        android:background="@drawable/rack"
        android:layout_width="65dp"
        android:gravity="center"
        android:layout_height="25dp"
        android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#000000" android:textStyle="bold" android:typeface="sans"/>
<TextView
        android:id="@+id/txt2"
        android:background="@drawable/rack"
        android:layout_width="65dp"
        android:gravity="center"
        android:layout_height="25dp"
        android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#000000" android:textStyle="bold" android:typeface="sans"/>
<TextView
        android:id="@+id/txt3"
        android:background="@drawable/rack"
        android:layout_width="65dp"
        android:gravity="center"
        android:layout_height="25dp"
        android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#000000" android:textStyle="bold" android:typeface="sans"/>
    <TextView
        android:id="@+id/txt1"
        android:background="@drawable/rack"
        android:layout_width="65dp"
        android:gravity="center"
        android:layout_height="25dp"
        android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#000000" android:textStyle="bold" android:typeface="sans"/>

    </LinearLayout>

    <Gallery
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/examplegallery"
        android:layout_width="242dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

可能是本节中的问题

gallery.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView parent, View v, int position, long id) {
// Displaying the position when the gallery item in clicked
    if (position == 0) {
        //Toast.makeText(Maingame.this, "heart", Toast.LENGTH_SHORT).show();
        TextView select = (TextView) findViewById(R.id.txt1);
          select.setText("heart"); 

    } else if (position == 1) {
        //Toast.makeText(Maingame.this, "lungs", Toast.LENGTH_SHORT).show();
        TextView select = (TextView) findViewById(R.id.txt2);
          select.setText("lungs");
    } else if (position == 2) {
        //Toast.makeText(Maingame.this, "brain", Toast.LENGTH_SHORT).show();
        TextView select = (TextView) findViewById(R.id.txt3);
          select.setText("brain");


    }
}
});

您的txt1,txt2,txt3在哪里我猜它在maingame.xml your textViews中使OnCreate中的onItemClick的intit在{{1}}中公开访问。