gpt4 book ai didi

Android Studio CountDownTimer 导航

转载 作者:行者123 更新时间:2023-11-30 01:51:10 25 4
gpt4 key购买 nike

我正在尝试开发一个应用程序,其中在一个布局上只有一个开始按钮,在按下开始按钮后,它会导航到另一个布局,倒数计时器将在该布局上自动开始运行。我能够实现倒数计时器,但是当我在按下开始按钮后尝试导航到另一个 Activity 时,应用程序崩溃了。请告诉我是否有办法做到这一点。谢谢

布局一的代码:只有开始按钮

<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:clickable="true"
android:onClick="action1" />

布局 2 的代码:

<TextView
android:id="@+id/counter"
android:layout_width="100dp"
android:layout_height="100dp"
android:textStyle="bold"
android:textSize="50sp"
android:gravity="center"
android:layout_marginLeft="250dp"
android:layout_marginTop="50dp" />

最后是 Java 文件:

public class MainActivity extends AppCompatActivity {

Button buttonStart;
TextView textCounter;

MyCountDownTimer myCountDownTimer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_countdown);

buttonStart = (Button)findViewById(R.id.start);
textCounter = (TextView)findViewById(R.id.counter);

buttonStart.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
myCountDownTimer = new MyCountDownTimer(30000, 1000);
myCountDownTimer.start();
}});

}

public class MyCountDownTimer extends CountDownTimer {

public MyCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}

@Override
public void onTick(long millisUntilFinished) {
textCounter.setText(millisUntilFinished / 1000 + " seconds remaining : ");

}

@Override
public void onFinish() {
textCounter.setText("Finished");

}

}



@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_main, 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);
}


public void action1(View View){setContentView(R.layout.activity_main);}

这些是我在 logcat 中收到的错误

--------- beginning of crash
10-09 03:36:02.651 2183-2183/com.fluke.kgwee.flukegame E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.fluke.kgwee.flukegame, PID: 2183
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.fluke.kgwee.flukegame.MainActivity$MyCountDownTimer.onTick(MainActivity.java:46)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:133)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

最佳答案

试试这个代码:布局一的代码: activity_main.xml ,只有开始按钮

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:id="@+id/start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:clickable="true"
android:text="Start" />

</LinearLayout>

Layout 2的代码:count_down_activity.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/counter"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="50dp"
android:layout_marginTop="50dp"
android:gravity="center"
android:textSize="50sp"
android:textStyle="bold" />

</LinearLayout>

Java 文件:MainActivity.java

    import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
Button startBrowser = (Button) findViewById(R.id.start);
startBrowser.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

startActivity(new Intent(mContext, CountDownActivity.class));
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action
// bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

Java 文件 2:CountDownActivity.java

    import android.app.Activity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.widget.TextView;

public class CountDownActivity extends Activity {

TextView textCounter;
MyCountDownTimer myCountDownTimer;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.count_down_activity);

textCounter = (TextView)findViewById(R.id.counter);

myCountDownTimer = new MyCountDownTimer(30000, 1000, textCounter);
myCountDownTimer.start();

}
public class MyCountDownTimer extends CountDownTimer {

private TextView textCounter;
public MyCountDownTimer(long millisInFuture, long countDownInterval, TextView textCounter) {
super(millisInFuture, countDownInterval);
this.textCounter = textCounter;
}

@Override
public void onTick(long millisUntilFinished) {
this.textCounter.setText(millisUntilFinished / 1000l + " seconds remaining : ");

}

@Override
public void onFinish() {
this.textCounter.setText("Finished");

}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action
// bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

AndroidManifest.xml

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

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.androidsample.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.androidsample.CountDownActivity"
android:screenOrientation="portrait" >
</activity>

</application>

</manifest>

关于Android Studio CountDownTimer 导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33029052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com