java - Android Studio 模拟器未运行。代码没问题

标签 java android

我正在为我的公司编写一个应用程序,但由于我的模拟器未运行,我无法看到我的工作。

下面是我的代码。没有错误。我正在 android studio 中运行它。

它可以在某些应用程序上运行,但我尝试输入我的代码,但它不起作用。

请帮忙

 package com.example.tankoverfill;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


Button calc;

TextView tv_heightfills = findViewById(R.id.tv_heightfills);
TextView tv_timefills = findViewById(R.id.tv_timefills);

EditText flow, areas, heights, densities, heightones, cvs;
int x1,y1,w1,a1, x2,y2,w2,a2,g;



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


    flow = findViewById(R.id.flow);
    areas = findViewById(R.id.areas);
    heights =  findViewById(R.id.heights);
    densities =  findViewById(R.id.densities);
    heightones  =  findViewById(R.id.heightones);
    cvs  =  findViewById(R.id.cvs);
    calc = findViewById(R.id.calc);



    calc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

        x1 = Integer.parseInt(flow.getText().toString());
        y1 = Integer.parseInt(cvs.getText().toString());
        w1 = Integer.parseInt(densities.getText().toString());
         g = (int) 9.8;

        a1 = (x1 / (y1*w1*g));

        tv_heightfills.setText(a1);

        }
    });

    calc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            x2 = Integer.parseInt(areas.getText().toString());
            y2 = Integer.parseInt(cvs.getText().toString());
           w2 = Integer.parseInt(densities.getText().toString());
             g = (int) 9.8;

    a2 = (x1 / (y1*w1*g));

    tv_timefills.setText(a2);



  }
 });

 }
 }

D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.tankoverfill, PID: 10137 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.tankoverfill/com.example.tankoverfill.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference at android.support.v7.app.AppCompatDelegateImpl.(AppCompatDelegateImpl.java:249) at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182) at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520) at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191) at com.example.tankoverfill.MainActivity.(MainActivity.java:15) at java.lang.Class.newInstance(Native Method) at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69) at android.support.v4.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43) at android.app.Instrumentation.newActivity(Instrumentation.java:1215) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loop(Looper.java:193)  at android.app.ActivityThread.main(ActivityThread.java:6669)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)  I/Process: Sending signal. PID: 10137 SIG: 9 Application terminated.

最佳答案

您好,您的代码中存在两个错误:

第一个错误是您必须在 onCreate() 方法中将两个 textview 链接到 xml

错误

TextView tv_heightfills = findViewById(R.id.tv_heightfills);
TextView tv_timefills = findViewById(R.id.tv_timefills);

正确

TextView tv_heightfills;
TextView tv_timefills;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv_heightfills = findViewById(R.id.tv_heightfills);
    tv_timefills = findViewById(R.id.tv_timefills);

    ....

}

第二个错误是将 int 值设置为 textview setText() 方法

所以应该是:

tv_heightfills.setText(String.valueOf(a1));

同样

tv_timefills.setText(String.valueOf(a2));

关于java - Android Studio 模拟器未运行。代码没问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56324408/

相关文章:

android - Android中数组适配器和游标适配器的区别

android - 如何通过布局 xml 文件更改 ExpandableListView 中子分隔符的颜色?

android - 无法理解 Intent 过滤器

java - 比较两个 arraylist 对象不起作用

android - 如何使用 ProGuard 追溯水平合并的类?

java - 在 Android 上的两个 Activity 之间切换时出现 NullPointerException

java - Java 中的 getGenericSuperclass()。它是如何工作的?

android - 如何在android studio中安装Uber的nullaway静态分析工具?

java - System.currentTimeMillis 与 System.nanoTime

java - 用Java将两个列表合并为一个“二维”列表