首页 > 解决方案 > Android Studio 模拟器未运行。代码没问题

问题描述

我正在为我的公司编写一个应用程序,但我看不到我的工作,因为我的模拟器没有运行。

下面是我的代码。没有错误。我在 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.app.ActivityThread.performLaunchActivity(ActivityThread.java: 2843) 在 android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 在 android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) ) 在 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) 原因: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(Native Method) 在 android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java: 69) 在 android.app.Instrumentation.newActivity(Instrumentation.java:1215) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831) 的 android.support.v4.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43) ) 在 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.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loop(Looper.java: 193) 在 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 应用程序终止。

标签: javaandroid

解决方案


您好,您的代码中有两个错误是:

第一个错误,您必须在 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));

推荐阅读