Android Studio模拟器未运行。代码还可以

时间:2019-05-27 10:37:38

标签: 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:关闭VM E / AndroidRuntime:致命异常:   主要       流程:com.example.tankoverfill,PID:10137       java.lang.RuntimeException:无法实例化活动ComponentInfo {com.example.tankoverfill / com.example.tankoverfill.MainActivity}:   java.lang.NullPointerException:尝试调用虚拟方法   上的“ android.view.Window $ Callback android.view.Window.getCallback()”   空对象引用           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2843)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)           在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1808)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6669)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)        原因:java.lang.NullPointerException:尝试调用虚拟方法'android.view.Window $ Callback   空对象引用上的android.view.Window.getCallback()'           在android.support.v7.app.AppCompatDelegateImpl。(AppCompatDelegateImpl.java:249)           在android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182)           在android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)           在android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)           在com.example.tankoverfill.MainActivity。(MainActivity.java:15)           在java.lang.Class.newInstance(本地方法)           在android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)           在android.support.v4.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43)           在android.app.Instrumentation.newActivity(Instrumentation.java:1215)           在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)           在android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)           在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)           在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1808)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:193)           在android.app.ActivityThread.main(ActivityThread.java:6669)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:493)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)   I / Process:正在发送信号。 PID:10137 SIG:9应用程序终止。

1 个答案:

答案 0 :(得分:0)

您好,您在代码中遇到的两个错误是:

第一个错误,您必须将两个textview链接到onCreate()方法中的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));