Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8326521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:47:20+00:00 2026-06-09T00:47:20+00:00

I made an activity for a game that will select images base on the

  • 0

I made an activity for a game that will select images base on the required part. But when i add chronometer to control the time of my activity, it makes an error and it force close the activity. But on my code, there’s no error when i add the code for chronometer. On my Logcat it says: “Uncaught handler: thread main exiting due to uncaught exception” and many more error on Logcat. Here’s my code, please help me find the problem, thank’s in advance!

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;
    }
    }
    }

Here’s my 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

Here’s my 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:47:22+00:00Added an answer on June 9, 2026 at 12:47 am

    may be the problem is in this section

    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");
    
    
        }
    }
    });
    

    Where is your txt1,txt2,txt3 I guess It is in maingame.xml Make the intit of your textViews in your OnCreate make them public to access in the onItemClick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've made an activity that looks a lot like the level select screen from
I have a silly little game that I made just for myself, but I
i wanna read file in an other activity but i made it in another
I made a list such that every time I click on an item, it
I have an activity that consists of textviews. I have made the activity compatible
i have made an app that would calculate numbers from input fields. **main Activity
I made an app that is just a simple counter that will keep track
I have made a shared preference activity that store the user settings, now i
For MS CRM 2011 I have made custom workflow activity, but I'm not able
I made a splash image to show at the start of my activity.. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.