Reputation: 51
The program should work like this, having a timer and a picture shown, using the bottom called "Age 10-11" should swap the user to another screen with the same setup of timer, but with different pictures being shown. The problem is it crashed when it comes to the intent part of the code. it works fine if the second activity class has only its default code, but when adding the same code from activity 1 it crashes. Message "Unfortunately, program has stopped working" Added the log
public class Age_7_to_9 extends Activity implements OnClickListener {
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button StartB;
private Button NextB;
public TextView text;
private final long startTime = 30* 1000;
private final long interval = 1* 1000;
ImageView imageView1;
int len=images.length-1;
static int curr=0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_age_7_to_9);
imageView1=(ImageView)findViewById(R.id.imageView);
StartB = (Button) this.findViewById(R.id.button);
NextB = (Button) this.findViewById(R.id.tenEleven);
StartB.setOnClickListener(this);
NextB.setOnClickListener(this);
text = (TextView) this.findViewById(R.id.timer);
countDownTimer = new MyCountDownTimer(startTime, interval);
text.setText(text.getText() + String.valueOf(startTime / 1000));
}
private static final int[] images=new int[] {R.drawable.p,R.drawable.pp,};
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.button:
if(curr < 2)
{
imageView1.setImageResource(images[curr]);
}
else
{
curr=-1;
}
curr++;
if(!timerHasStarted)
{
countDownTimer.start();
timerHasStarted = true;
StartB.setText("STOP");
}
else
{
countDownTimer.cancel();
timerHasStarted = false;
StartB.setText("RESTART");
Log.i("MyActivity", "MyClass.getView() — get item number " + curr);
}
break;
case R.id.tenEleven:
Intent i = new Intent(this, Age_10_to_11.class);
startActivity(i);
break;
}
}
public class MyCountDownTimer extends CountDownTimer
{
public MyCountDownTimer(long startTime, long interval)
{
super(startTime, interval);
}
@Override
public void onFinish()
{
text.setText("Time's Up!");
}
@Override
public void onTick(long millisUntilFinished)
{
text.setText(""+ millisUntilFinished / 1000);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_age_7_to_9, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
XML code
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Age_7_to_9"
android:background="@drawable/backgroundimage">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="99dp" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age 10-11"
android:id="@+id/tenEleven"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="onClick"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/timer"
android:layout_above="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="51dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:layout_above="@+id/timer"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Highscore:"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:id="@+id/textView2"
android:layout_alignTop="@+id/textView4"
android:layout_alignRight="@+id/tenEleven"
android:layout_alignEnd="@+id/tenEleven" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Counter:"
android:id="@+id/textView3"
android:layout_alignTop="@+id/textView2"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="0"
android:id="@+id/textView4"
android:layout_alignTop="@+id/textView"
android:layout_toLeftOf="@+id/button"
android:layout_toStartOf="@+id/button" />
</RelativeLayout>
Log below
04-29 10:29:00.390 2198-2198/com.example.daniel.pmp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.daniel.pmp, PID: 2198
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.daniel.pmp/com.example.daniel.pmp.Age_10_to_11}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at com.example.daniel.pmp.Age_10_to_11.onCreate(Age_10_to_11.java:46)
at android.app.Activity.performCreate(Activity.java:5937)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Upvotes: 0
Views: 571
Reputation: 9870
Without seeing Logcat it will be difficult to know, but if I look into Your code, I see some things going wrong here. Don´t know if it is just a copy/paste mistake.
You are initializing len
BEFORE initializing images
:
int len=images.length-1;
after that You are initializing images:
private static final int[] images=new int[] {R.drawable.p,R.drawable.pp,};
That will not work, You can´t get a length if images is not initialized. That leads us to the second mistake:
Your images array has a comma at the end of the list:
{R.drawable.p,R.drawable.pp,} <-- that could not work.
Also, You are using the wrong parameter here:
case R.id.tenEleven:
Intent i = new Intent(this, Age_10_to_11.class);
startActivity(i);
break;
instead of "this" it must be Age_7_to_9.this
:
case R.id.tenEleven:
Intent i = new Intent(Age_7_to_9.this, Age_10_to_11.class);
startActivity(i);
break;
otherwise You will reference the OnClickListener.
Because of these three mistakes, usually Your app would not compile. But what I guess what is causing Your crash is, that You don´t have added the Age_10_to_11
Activity to Your manifest.But to know it exactly, You have to post Your logcat...
Upvotes: 1