我搜索了它,但任何解决方案都无法解决我的问题。 我正在片段中实现一个计时器,但我的应用程序停止工作显示错误。
这是我的应用程序的工作。 当用户单击开始按钮时,它会将用户带到下一个活动,其中片段显示在容器中(RelativeLayout)。我在片段中实现了Count CountDownTimer函数。片段在CountDownTimer函数之前完美运行。
任何帮助? 这是代码。
Start.java
package com.example.sheikhspc.timerexample;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class Start extends AppCompatActivity {
Fragment frag = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
frag = new BlankFragment1();
fragmentTransaction.replace(R.id.container,frag);
fragmentTransaction.commit();
}
}
MainActivity.java
package com.example.sheikhspc.timerexample;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)findViewById(R.id.startbtn);
}
public void start(View view)
{
Intent intent = new Intent(this,Start.class);
startActivity(intent);
}
}
BlankFragment1.java
package com.example.sheikhspc.timerexample;
import android.os.Bundle;
import android.app.Fragment;
import android.os.CountDownTimer;
import android.provider.DocumentsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
public class BlankFragment1 extends Fragment {
public BlankFragment1() {
// Required empty public constructor
}
Fragment frag = null;
TextView tv;
View root;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
tv = (TextView)root.findViewById(R.id.timer1);
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
tv.setText("seconds remaining: " + millisUntilFinished / 1000);
//here you can have your logic to set text to edittext
}
public void onFinish() {
tv.setText("done!");
}
}.start();
return inflater.inflate(R.layout.fragment_blank_fragment1, container, false);
}
}
错误记录
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sheikhspc.timerexample, PID: 28970
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sheikhspc.timerexample/com.example.sheikhspc.timerexamp le.Start}:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at com.example.sheikhspc.timerexample.BlankFragment1.onCreateView(BlankFragment1.java:46)
at android.app.Fragment.performCreateView(Fragment.java:2053)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:834)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1454)
at android.app.Activity.performStart(Activity.java:6025)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
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:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
Application terminated.
答案 0 :(得分:1)
On OnCreateView
View v = inflater.inflate(R.layout.fragment_blank_fragment1, null);
然后按v.findViewById
查找项目并返回夸大的View
return v;
或者您可以通过
将布局分配给rootroot = inflater.inflate(R.layout.fragment_blank_fragment1, null);
tv = (TextView) root.findViewById(...);
// other implementations
return root;