当我尝试在onTick()中设置文本时收到NullPointerException时,我的代码如下。
public class TrainingProcessFragent extends SherlockFragment implements
OnClickListener {
public TextView text;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_training_process, null);
text = (TextView) v.findViewById(R.id.tvTimer1);
text.setText(" " + min + " : " + sec);
text.setOnClickListener(this);
showCurrentSituation();
return v;
}
@Override
public void onClick(View v) {
db.open();
Intent intent;
switch (v.getId()) {
case R.id.tvTimer1:
showDialog();
break;
}
}
public void doPositiveClick(long time) {
DialogFlag = 0;
startTime = time;
Log.d("myLogs", String.valueOf(startTime));
countDownTimer = new MyCountDownTimer(startTime, 1000);
countDownTimer.start();
// dialog.cancel();
}
public void doNegativeClick() {
DialogFlag = 1;
}
void showDialog() {
newFragment.show(getFragmentManager(), "dialog");
}
public void onStop() {
db.close();
super.onStop();
}
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) {
millisUntilFinished /= 1000;
// hour = Long.toString(temp / 3600);
min = Long.toString((millisUntilFinished) / 60);
sec = Long.toString(millisUntilFinished - Long.valueOf(min) * 60);
if (sec.length() == 1) {
sec = "0" + sec;
}
if (min.length() == 1) {
min = "0" + min;
}
text.setText(" " + min + " : " + sec);//ERROR
Log.d("myLogs", " " + min + " : " + sec);// All is correct
// text.setText("" + millisUntilFinished / 1000);
}
}
布局代码
<?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:background="@drawable/background"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="59dp" >
<TextView
android:id="@+id/tv_ex_name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="4.94"
android:gravity="center"
android:textColor="#4f4f4f"
android:textSize="20sp"
android:textStyle="bold" />
<ImageButton
android:id="@+id/imb_ex_info"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:background="@android:color/transparent"
android:scaleType="fitCenter"
android:src="@drawable/btn_info" />
<ImageButton
android:id="@+id/imbtn_history"
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:scaleType="fitCenter"
android:src="@drawable/btn_calendar" />
<TextView
android:id="@+id/tvTimer1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="4.94"
android:background="@color/abs__holo_blue_light"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Поточна вправа:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tv_current_exercise_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#4f4f4f"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Номер підходу"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tv_current_set"
android:layout_width="198dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#4f4f4f"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Необхідно зробити "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tv_repeats"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#4f4f4f"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" повторень"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/pr_date"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Вага "
android:textSize="25sp"
android:textStyle="bold" />
<Spinner
android:id="@+id/spinner_weigth"
android:layout_width="155dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/pr_weigth"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Повторення "
android:textSize="25sp"
android:textStyle="bold" />
<Spinner
android:id="@+id/spinner_repeats"
android:layout_width="155dp"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="@+id/btn_next_set"
android:layout_width="match_parent"
android:layout_height="93dp"
android:layout_marginTop="20dp"
android:background="@android:color/transparent"
android:scaleType="fitCenter"
android:src="@drawable/btn_next" />
</LinearLayout>
</LinearLayout>
堆栈跟踪 :
08-02 12:50:19.954: I/dalvikvm(2274): Could not find method com.actionbarsherlock.internal.nineoldandroids.widget.NineFrameLayout.onHoverEvent, referenced from method com.actionbarsherlock.internal.widget.ActionBarContainer.onHoverEvent
08-02 12:50:19.975: W/dalvikvm(2274): VFY: unable to resolve virtual method 5970: Lcom/actionbarsherlock/internal/nineoldandroids/widget/NineFrameLayout;.onHoverEvent (Landroid/view/MotionEvent;)Z
08-02 12:50:19.975: D/dalvikvm(2274): VFY: replacing opcode 0x6f at 0x0000
08-02 12:50:19.975: D/dalvikvm(2274): VFY: dead code 0x0003-0004 in Lcom/actionbarsherlock/internal/widget/ActionBarContainer;.onHoverEvent (Landroid/view/MotionEvent;)Z
08-02 12:50:20.004: I/dalvikvm(2274): Could not find method android.widget.FrameLayout.getAlpha, referenced from method com.actionbarsherlock.internal.nineoldandroids.widget.NineFrameLayout.getAlpha
08-02 12:50:20.004: W/dalvikvm(2274): VFY: unable to resolve virtual method 4637: Landroid/widget/FrameLayout;.getAlpha ()F
08-02 12:50:20.004: D/dalvikvm(2274): VFY: replacing opcode 0x6f at 0x000b
08-02 12:50:20.004: D/dalvikvm(2274): VFY: dead code 0x000e-000f in Lcom/actionbarsherlock/internal/nineoldandroids/widget/NineFrameLayout;.getAlpha ()F
08-02 12:50:20.004: I/dalvikvm(2274): Could not find method android.widget.FrameLayout.getTranslationY, referenced from method com.actionbarsherlock.internal.nineoldandroids.widget.NineFrameLayout.getTranslationY
08-02 12:50:20.004: W/dalvikvm(2274): VFY: unable to resolve virtual method 4638: Landroid/widget/FrameLayout;.getTranslationY ()F
08-02 12:50:20.004: D/dalvikvm(2274): VFY: replacing opcode 0x6f at 0x000b
08-02 12:50:20.004: D/dalvikvm(2274): VFY: dead code 0x000e-000f in Lcom/actionbarsherlock/internal/nineoldandroids/widget/NineFrameLayout;.getTranslationY ()F
08-02 12:50:20.014: I/dalvikvm(2274): Could not find method android.widget.FrameLayout.setAlpha, referenced from method com.actionbarsherlock.internal.nineoldandroids.widget.NineFrameLayout.setAlpha
08-02 12:50:20.014: W/dalvikvm(2274): VFY: unable to resolve virtual method 4641: Landroid/widget/FrameLayout;.setAlpha (F)V
08-02 12:50:20.014: D/dalvikvm(2274): VFY: replacing opcode 0x6f at 0x000a
08-02 12:50:20.014: D/dalvikvm(2274): VFY: dead code 0x000d-000d in Lcom/actionbarsherlock/internal/nineoldandroids/widget/NineFrameLayout;.setAlpha (F)V
08-02 12:50:20.014: I/dalvikvm(2274): Could not find method android.widget.FrameLayout.setTranslationY, referenced from method com.actionbarsherlock.internal.nineoldandroids.widget.NineFrameLayout.setTranslationY
08-02 12:50:20.014: W/dalvikvm(2274): VFY: unable to resolve virtual method 4649: Landroid/widget/FrameLayout;.setTranslationY (F)V
08-02 12:50:20.014: D/dalvikvm(2274): VFY: replacing opcode 0x6f at 0x000a
08-02 12:50:20.014: D/dalvikvm(2274): VFY: dead code 0x000d-000d in Lcom/actionbarsherlock/internal/nineoldandroids/widget/NineFrameLayout;.setTranslationY (F)V
08-02 12:50:20.054: I/dalvikvm(2274): Could not find method android.view.ViewGroup.getAlpha, referenced from method com.actionbarsherlock.internal.nineoldandroids.view.NineViewGroup.getAlpha
08-02 12:50:20.054: W/dalvikvm(2274): VFY: unable to resolve virtual method 4366: Landroid/view/ViewGroup;.getAlpha ()F
08-02 12:50:20.054: D/dalvikvm(2274): VFY: replacing opcode 0x6f at 0x000b
08-02 12:50:20.054: D/dalvikvm(2274): VFY: dead code 0x000e-000f in Lcom/actionbarsherlock/internal/nineoldandroids/view/NineViewGroup;.getAlpha ()F
08-02 12:50:20.054: I/dalvikvm(2274): Could not find method android.view.ViewGroup.getTranslationX, referenced from method com.actionbarsherlock.internal.nineoldandroids.view.NineViewGroup.getTranslationX
08-02 12:50:20.064: W/dalvikvm(2274): VFY: unable to resolve virtual method 4377: Landroid/view/ViewGroup;.getTranslationX ()F
08-02 12:50:20.064: D/dalvikvm(2274): VFY: replacing opcode 0x6f at 0x000b
08-02 12:50:20.064: D/dalvikvm(2274): VFY: dead code 0x000e-000f in Lcom/actionbarsherlock/internal/nineoldandroids/view/NineViewGroup;.getTranslationX ()F
08-02 12:50:20.064: I/dalvikvm(2274): Could not find method android.view.ViewGroup.getTranslationY, referenced from method com.actionbarsherlock.internal.nineoldandroids.view.NineViewGroup.getTranslationY
08-02 12:50:20.064: W/dalvikvm(2274): VFY: unable to resolve virtual method 4378: Landroid/view/ViewGroup;.getTranslationY ()F
08-02 12:50:20.064: D/dalvikvm(2274): VFY: replacing opcode 0x6f at 0x000b
08-02 12:50:20.074: D/dalvikvm(2274): VFY: dead code 0x000e-000f in Lcom/actionbarsherlock/internal/nineoldandroids/view/NineViewGroup;.getTranslationY ()F
08-02 12:50:20.074: I/dalvikvm(2274): Could not find method android.view.ViewGroup.setAlpha, referenced from method com.actionbarsherlock.internal.nineoldandroids.view.NineViewGroup.setAlpha
08-02 12:50:20.074: W/dalvikvm(2274): VFY: unable to resolve virtual method 4400: Landroid/view/ViewGroup;.setAlpha (F)V
08-02 12:50:20.074: D/dalvikvm(2274): VFY: replacing opcode 0x6f at 0x000a
08-02 12:50:20.074: D/dalvikvm(2274): VFY: dead code 0x000d-000d in Lcom/actionbarsherlock/internal/nineoldandroids/view/NineViewGroup;.setAlpha (F)V
08-02 12:50:20.074: I/dalvikvm(2274): Could not find method android.view.ViewGroup.setTranslationX, referenced from method com.actionbarsherlock.internal.nineoldandroids.view.NineViewGroup.setTranslationX
08-02 12:50:20.074: W/dalvikvm(2274): VFY: unable to resolve virtual method 4405: Landroid/view/ViewGroup;.setTranslationX (F)V
08-02 12:50:20.074: D/dalvikvm(2274): VFY: replacing opcode 0x6f at 0x000a
08-02 12:50:20.074: D/dalvikvm(2274): VFY: dead code 0x000d-000d in Lcom/actionbarsherlock/internal/nineoldandroids/view/NineViewGroup;.setTranslationX (F)V
08-02 12:50:20.074: I/dalvikvm(2274): Could not find method android.view.ViewGroup.setTranslationY, referenced from method com.actionbarsherlock.internal.nineoldandroids.view.NineViewGroup.setTranslationY
08-02 12:50:20.074: W/dalvikvm(2274): VFY: unable to resolve virtual method 4406: Landroid/view/ViewGroup;.setTranslationY (F)V
08-02 12:50:20.074: D/dalvikvm(2274): VFY: replacing opcode 0x6f at 0x000a
08-02 12:50:20.084: D/dalvikvm(2274): VFY: dead code 0x000d-000d in Lcom/actionbarsherlock/internal/nineoldandroids/view/NineViewGroup;.setTranslationY (F)V
08-02 12:50:20.234: D/dalvikvm(2274): GC_EXTERNAL_ALLOC freed 116K, 49% free 2806K/5447K, external 1537K/1559K, paused 125ms
08-02 12:50:20.335: I/dalvikvm(2274): Could not find method com.actionbarsherlock.internal.widget.ActionBarView$HomeView.onHoverEvent, referenced from method com.actionbarsherlock.internal.widget.ActionBarView$HomeView.dispatchHoverEvent
08-02 12:50:20.335: W/dalvikvm(2274): VFY: unable to resolve virtual method 6831: Lcom/actionbarsherlock/internal/widget/ActionBarView$HomeView;.onHoverEvent (Landroid/view/MotionEvent;)Z
08-02 12:50:20.335: D/dalvikvm(2274): VFY: replacing opcode 0x6e at 0x0000
08-02 12:50:20.335: D/dalvikvm(2274): VFY: dead code 0x0003-0004 in Lcom/actionbarsherlock/internal/widget/ActionBarView$HomeView;.dispatchHoverEvent (Landroid/view/MotionEvent;)Z
08-02 12:50:20.335: I/dalvikvm(2274): Could not find method android.widget.FrameLayout.onPopulateAccessibilityEvent, referenced from method com.actionbarsherlock.internal.widget.ActionBarView$HomeView.onPopulateAccessibilityEvent
08-02 12:50:20.345: W/dalvikvm(2274): VFY: unable to resolve virtual method 4640: Landroid/widget/FrameLayout;.onPopulateAccessibilityEvent (Landroid/view/accessibility/AccessibilityEvent;)V
08-02 12:50:20.345: D/dalvikvm(2274): VFY: replacing opcode 0x6f at 0x0006
08-02 12:50:20.545: D/dalvikvm(2274): GC_EXTERNAL_ALLOC freed 42K, 49% free 2831K/5447K, external 2067K/2370K, paused 46ms
08-02 12:50:20.675: D/dalvikvm(2274): GC_EXTERNAL_ALLOC freed 9K, 48% free 2839K/5447K, external 2431K/2822K, paused 63ms
08-02 12:50:20.845: D/dalvikvm(2274): GC_EXTERNAL_ALLOC freed 10K, 48% free 2848K/5447K, external 3592K/4441K, paused 45ms
08-02 12:50:21.384: D/myLogs(2274): true
08-02 12:50:21.394: D/myLogs(2274): true
08-02 12:50:21.605: I/dalvikvm(2274): Could not find method android.view.View.jumpDrawablesToCurrentState, referenced from method com.actionbarsherlock.internal.view.menu.BaseMenuPresenter.updateMenuView
08-02 12:50:21.605: W/dalvikvm(2274): VFY: unable to resolve virtual method 4273: Landroid/view/View;.jumpDrawablesToCurrentState ()V
08-02 12:50:21.605: D/dalvikvm(2274): VFY: replacing opcode 0x6e at 0x0056
08-02 12:50:21.614: I/dalvikvm(2274): Could not find method android.widget.LinearLayout.getAlpha, referenced from method com.actionbarsherlock.internal.nineoldandroids.widget.NineLinearLayout.getAlpha
08-02 12:50:21.614: W/dalvikvm(2274): VFY: unable to resolve virtual method 4692: Landroid/widget/LinearLayout;.getAlpha ()F
08-02 12:50:21.624: D/dalvikvm(2274): VFY: replacing opcode 0x6f at 0x000b
08-02 12:50:21.624: D/dalvikvm(2274): VFY: dead code 0x000e-000f in Lcom/actionbarsherlock/internal/nineoldandroids/widget/NineLinearLayout;.getAlpha ()F
08-02 12:50:21.624: I/dalvikvm(2274): Could not find method android.widget.LinearLayout.getTranslationX, referenced from method com.actionbarsherlock.internal.nineoldandroids.widget.NineLinearLayout.getTranslationX
08-02 12:50:21.624: W/dalvikvm(2274): VFY: unable to resolve virtual method 4695: Landroid/widget/LinearLayout;.getTranslationX ()F
08-02 12:50:21.624: D/dalvikvm(2274): VFY: replacing opcode 0x6f at 0x000b
08-02 12:50:21.624: D/dalvikvm(2274): VFY: dead code 0x000e-000f in Lcom/actionbarsherlock/internal/nineoldandroids/widget/NineLinearLayout;.getTranslationX ()F
08-02 12:50:21.624: I/dalvikvm(2274): Could not find method android.widget.LinearLayout.setAlpha, referenced from method com.actionbarsherlock.internal.nineoldandroids.widget.NineLinearLayout.setAlpha
08-02 12:50:21.624: W/dalvikvm(2274): VFY: unable to resolve virtual method 4707: Landroid/widget/LinearLayout;.setAlpha (F)V
08-02 12:50:21.624: D/dalvikvm(2274): VFY: replacing opcode 0x6f at 0x000a
08-02 12:50:21.624: D/dalvikvm(2274): VFY: dead code 0x000d-000d in Lcom/actionbarsherlock/internal/nineoldandroids/widget/NineLinearLayout;.setAlpha (F)V
08-02 12:50:21.624: I/dalvikvm(2274): Could not find method android.widget.LinearLayout.setTranslationX, referenced from method com.actionbarsherlock.internal.nineoldandroids.widget.NineLinearLayout.setTranslationX
08-02 12:50:21.624: W/dalvikvm(2274): VFY: unable to resolve virtual method 4713: Landroid/widget/LinearLayout;.setTranslationX (F)V
08-02 12:50:21.624: D/dalvikvm(2274): VFY: replacing opcode 0x6f at 0x000a
08-02 12:50:21.624: D/dalvikvm(2274): VFY: dead code 0x000d-000d in Lcom/actionbarsherlock/internal/nineoldandroids/widget/NineLinearLayout;.setTranslationX (F)V
08-02 12:50:21.644: W/KeyCharacterMap(2274): No keyboard for id -1
08-02 12:50:21.644: W/KeyCharacterMap(2274): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
08-02 12:50:21.684: D/gralloc_goldfish(2274): Emulator without GPU emulation detected.
08-02 12:50:21.785: D/dalvikvm(2274): GC_EXTERNAL_ALLOC freed 35K, 47% free 2941K/5511K, external 7087K/7483K, paused 57ms
08-02 12:50:23.404: D/dalvikvm(2274): GC_EXTERNAL_ALLOC freed 18K, 47% free 2938K/5511K, external 7057K/8813K, paused 52ms
08-02 12:50:23.745: D/myLogs(2274): onCreateView
08-02 12:50:23.815: D/myLogs(2274): set=1
08-02 12:50:23.825: D/myLogs(2274): 1 ( 12 12 12 12 )
08-02 12:50:23.825: D/myLogs(2274): 2 ( 15 15 )
08-02 12:50:23.825: D/myLogs(2274): 6 ( 12 12 12 12 )
08-02 12:50:23.825: D/myLogs(2274): 18 ( 15 15 )
08-02 12:50:25.771: D/myLogs(2274): Заебись
08-02 12:50:25.828: D/myLogs(2274): Заебись
08-02 12:50:25.966: D/myLogs(2274): Заебись
08-02 12:50:26.785: D/myLogs(2274): 62000
08-02 12:50:26.795: D/AndroidRuntime(2274): Shutting down VM
08-02 12:50:26.795: W/dalvikvm(2274): threadid=1: thread exiting with uncaught exception (group=0xb5feb4f0)
08-02 12:50:26.805: E/AndroidRuntime(2274): FATAL EXCEPTION: main
08-02 12:50:26.805: E/AndroidRuntime(2274): java.lang.NullPointerException
08-02 12:50:26.805: E/AndroidRuntime(2274): at com.example.gymforyou01.TrainingProcessFragent$MyCountDownTimer.onTick(TrainingProcessFragent.java:369)
08-02 12:50:26.805: E/AndroidRuntime(2274): at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
08-02 12:50:26.805: E/AndroidRuntime(2274): at android.os.Handler.dispatchMessage(Handler.java:99)
08-02 12:50:26.805: E/AndroidRuntime(2274): at android.os.Looper.loop(Looper.java:130)
08-02 12:50:26.805: E/AndroidRuntime(2274): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-02 12:50:26.805: E/AndroidRuntime(2274): at java.lang.reflect.Method.invokeNative(Native Method)
08-02 12:50:26.805: E/AndroidRuntime(2274): at java.lang.reflect.Method.invoke(Method.java:507)
08-02 12:50:26.805: E/AndroidRuntime(2274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-02 12:50:26.805: E/AndroidRuntime(2274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-02 12:50:26.805: E/AndroidRuntime(2274): at dalvik.system.NativeStart.main(Native Method)
最佳答案
确保行中有正确的R
:
text = (TextView) v.findViewById(R.id.tvTimer1);
有时您会获得默认的
R
,而不是从gen
文件夹中获得。另一方面,请确保在
onCreateView
之前先致电onTick
要么:
验证布局XML是否包含名为
tvTimer1
的TextView。关于android - 当我尝试在TextView中使用setText()时发生NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18018328/