Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 文本视图NullPointerExceptionn_Java_Android - Fatal编程技术网

Java 文本视图NullPointerExceptionn

Java 文本视图NullPointerExceptionn,java,android,Java,Android,运行此代码时,我遇到空指针异常: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); watch = (TextView)findViewById(R.id.wa); e = (EditText)findViewById(R.i

运行此代码时,我遇到空指针异常:

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        watch = (TextView)findViewById(R.id.wa);
        e = (EditText)findViewById(R.id.ed);
}
public  void setTextView(String t) {

        watch.setText("AAAAA");
        watch.setText(t);

    }
我通过以下方式调用另一个类上的方法setTextView:

 MainActivity m = new MainActivity();
    int counter=0 ;
    public void onTick(long millisUntilFinished) {
        counter ++;
        int min = counter/60;
        int hour = min/60;
        min = min%60;
        int sec = min%60;
        String t = new String ();
        if (hour<10){
            t+=("0"+hour);
        }
        else
            t+=hour;
        if (min<10){
            t+=("0"+min);
        }
        else
            t+=min;
        if (sec<10){
            t+=("0"+sec);
        }
        else
            t+=sec;
       m.setTextView(t+"");
    }
我尝试过反转setContentView和FindView,如下所示

watch = (TextView)findViewById(R.id.wa);
e = (EditText)findViewById(R.id.ed);

 setContentView(R.layout.activity_main);

但它不起作用我该怎么办?

这不是Android的工作方式:

MainActivity m=新的MainActivity()

您不应该为自己创建活动实例。它们是由系统创建的,这就是为什么您必须在清单中声明它们。启动活动的正确方法是使用上下文创建一个新的
Intent
对象。检查隐式和显式意图文档


这将正确地启动您的活动,调用onCreate方法并允许您准备视图。

您不应该这样做,您可以在构造函数中发送视图(您的TextView)并使用它(如注入模式)。使用它完成你的工作(单击Listener)。

请也发布日志。切勿在扩展活动的类上使用新运算符我正在尝试使用来自新类的文本视图。如何才能做到这一点?我无法在新类中使用findViewById,您不能做的是创建活动的实例。它是由系统创建的。除此之外,代码的其余部分看起来正常如果您有一个活动正在工作,那么这个活动不能对另一个活动执行任何操作,如果您正在尝试的话。您唯一能做的就是将参数传递给第二个活动,以解释它应该做什么
<EditText
                android:layout_width="216dp"
                android:layout_height="wrap_content"
                android:id="@+id/ed"
                android:layout_centerVertical="true"
                android:layout_alignParentEnd="true"
                android:inputType="text"
                android:layout_gravity="center_vertical" />
10-11 20:27:42.047 20651-20651/com.example.ahmed.stopwatch E/AndroidRuntime: FATAL EXCEPTION: main
10-11 20:27:42.047 20651-20651/com.example.ahmed.stopwatch E/AndroidRuntime: Process: com.example.ahmed.stopwatch, PID: 20651
10-11 20:27:42.047 20651-20651/com.example.ahmed.stopwatch E/AndroidRuntime: java.lang.NullPointerException
10-11 20:27:42.047 20651-20651/com.example.ahmed.stopwatch E/AndroidRuntime:     at com.example.ahmed.stopwatch.MainActivity.setTextView(MainActivity.java:70)
10-11 20:27:42.047 20651-20651/com.example.ahmed.stopwatch E/AndroidRuntime:     at com.example.ahmed.stopwatch.counterWatch.onTick(counterWatch.java:49)
10-11 20:27:42.047 20651-20651/com.example.ahmed.stopwatch E/AndroidRuntime:     at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:124)
10-11 20:27:42.047 20651-20651/com.example.ahmed.stopwatch E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
10-11 20:27:42.047 20651-20651/com.example.ahmed.stopwatch E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
10-11 20:27:42.047 20651-20651/com.example.ahmed.stopwatch E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5476)
10-11 20:27:42.047 20651-20651/com.example.ahmed.stopwatch E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
10-11 20:27:42.047 20651-20651/com.example.ahmed.stopwatch E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
10-11 20:27:42.047 20651-20651/com.example.ahmed.stopwatch E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
10-11 20:27:42.047 20651-20651/com.example.ahmed.stopwatch E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
10-11 20:27:42.047 20651-20651/com.example.ahmed.stopwatch E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)
watch = (TextView)findViewById(R.id.wa);
e = (EditText)findViewById(R.id.ed);

 setContentView(R.layout.activity_main);
Intent intent = new Intent(context, MainActivity.class);
context.startActivity(intent);