Uvs
Uvs

Reputation: 31

getting exception when starting new activity on an android

i have read a lot about this but didnt come up with a solution. this is my first android project.

i am trying yo open an activity from my main activity. i have doubled check the names and the configuration files, but the startactivity() procedure fails. debbuger shows runtime errror. as u will c, the startactivity is called upon onclick event.

i am really stuck here dont know what else to do.

thanks a lot!

* this is the main activity **

package com.example.helloandroid;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public  class HelloworldActivity extends Activity {
    /** Called when the activity is first created. */  
    private OnClickListener gtScoringListener = new OnClickListener() {
        public void onClick(View v) { 
            Intent mintent = new Intent(HelloworldActivity.this ,imgScoring.class);
            startActivity(mintent);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(gtScoringListener);
        }
}

* this is the target activity: *

package com.example.helloandroid;

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

public class imgScoring extends Activity {
    private OnClickListener backBtnListener = new OnClickListener() {
        public void onClick(View v) { 
            Intent intent = new Intent(imgScoring.this, HelloworldActivity.class);
            startActivity(intent);
            }
    };
    private OnClickListener scoreBtnListener = new OnClickListener() {
        public void onClick(View v){
            TextView tv = (TextView)findViewById(R.id.editText1);
            tv.setText(((Button)v).getText());
            }
    };
    /** Called when the activity is first created. */  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(scoreBtnListener);
        button = (Button)findViewById(R.id.Button01);
        button.setOnClickListener(scoreBtnListener);
        button = (Button)findViewById(R.id.Button03);
        button.setOnClickListener(scoreBtnListener);
        button = (Button)findViewById(R.id.Button02);
        button.setOnClickListener(scoreBtnListener);
        button = (Button)findViewById(R.id.Button04);
        button.setOnClickListener(scoreBtnListener);
        button = (Button)findViewById(R.id.button3);
        button.setOnClickListener(backBtnListener);

        setContentView(R.layout.imgscoring);
        }
}

this is the androidmanifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.helloandroid"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="12" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".imgScoring"></activity>
        <activity android:name=".HelloworldActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

Upvotes: 0

Views: 624

Answers (2)

radzio
radzio

Reputation: 2892

Try change class name from imgScoring to ImgScoring everywhere. Java is not allowing class name to start from small letter ;-)

And place setContentView(R.layout.imgscoring); just after your super.onCreate();

Upvotes: 0

Nanne
Nanne

Reputation: 64399

You should've posted the stacktrace, but I assume the error is on this line in your target activity:

button.setOnClickListener(scoreBtnListener);

This will give you a NULL:

Button button = (Button)findViewById(R.id.button1);

because you haven't set a contentview in THAT activity, so there is nothing for findViewById() to return. You'll get a nullpointer when calling that setOnclickLIstener()

Upvotes: 3

Related Questions