i am Newbie and i am receiving errors and i don´t know why D:
When i press Button my app crashes. Here my code:
I tried different versions of countdowns and no one runs D: Thanks...
MainActivity
package com.example.chrontest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button Start = (Button) findViewById(R.id.button1);
Start.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Jungle CuentaAtras = new Jungle(6000, 1000);
CuentaAtras.start();
}});
}
}
Jungle.class
package com.example.chrontest;
import android.os.CountDownTimer;
import android.widget.TextView;
public class Jungle extends CountDownTimer{
TextView Texto = (TextView) findViewById(R.id.textView1);
public Jungle(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
// TODO Auto-generated constructor stub
}
private TextView findViewById(int textview1) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
Texto.setText("GAME OVER");
}
@Override
public void onTick(long millisUntilFinished) {
Texto.setText ("" + millisUntilFinished / 1000);
}
}
And here my LogCat:
12-03 04:13:41.175: D/gralloc_goldfish(1395): Emulator without GPU emulation detected.
12-03 04:13:52.325: D/AndroidRuntime(1395): Shutting down VM
12-03 04:13:52.325: W/dalvikvm(1395): threadid=1: thread exiting with uncaught exception (group=0x41465700)
12-03 04:13:52.345: E/AndroidRuntime(1395): FATAL EXCEPTION: main
12-03 04:13:52.345: E/AndroidRuntime(1395): java.lang.NullPointerException
12-03 04:13:52.345: E/AndroidRuntime(1395): at com.example.chrontest.Jungle.onTick(Jungle.java:28)
12-03 04:13:52.345: E/AndroidRuntime(1395): at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
12-03 04:13:52.345: E/AndroidRuntime(1395): at android.os.Handler.dispatchMessage(Handler.java:99)
12-03 04:13:52.345: E/AndroidRuntime(1395): at android.os.Looper.loop(Looper.java:137)
12-03 04:13:52.345: E/AndroidRuntime(1395): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-03 04:13:52.345: E/AndroidRuntime(1395): at java.lang.reflect.Method.invokeNative(Native Method)
12-03 04:13:52.345: E/AndroidRuntime(1395): at java.lang.reflect.Method.invoke(Method.java:525)
12-03 04:13:52.345: E/AndroidRuntime(1395): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-03 04:13:52.345: E/AndroidRuntime(1395): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-03 04:13:52.345: E/AndroidRuntime(1395): at dalvik.system.NativeStart.main(Native Method)
12-03 04:13:53.925: I/Process(1395): Sending signal. PID: 1395 SIG: 9
UPDATED:
Lol i don´t put my layout, and.. today i see that i don´t have a view or anything to see my countdown, or just this will appear in the screen, lol i don´t know :|
This is my layout:
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="34dp"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="Button" />
</RelativeLayout>
Your jungle
class is not an activity class
. So you cannot get the
TextView Texto = (TextView) findViewById(R.id.textView1);
here and use it
What you should do is calculate the countdown
and give a callback to your activity
class. There you should set the TextView
UPDATE
package com.example.chrontest;
import android.os.CountDownTimer;
import android.widget.TextView;
public class Jungle extends CountDownTimer{
Context mContext;
public Jungle(long millisInFuture, long countDownInterval, Context context) {
super(millisInFuture, countDownInterval);
mContext = context;
// TODO Auto-generated constructor stub
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
mcontext.MethodToSetText("YourText");
}
@Override
public void onTick(long millisUntilFinished) {
mcontext.MethodToSetText("" + millisUntilFinished / 1000);
}
}
And call
Jungle CuentaAtras = new Jungle(6000, 1000, this);