Android开发的新手:应用程序可在我的MacBook Pro上运行,但在iMac上出现Kotlin Typecast异常失败

时间:2018-09-23 16:32:37

标签: java android kotlin android-button

这是最奇怪的事情。几周前,我遇到了这个问题,发现解决方案是更新Gradle Runtime,这是我在两台计算机上完成的。我决定我想和我的iMac一起坐在办公桌前并昨晚工作,但无法让该应用程序运行到Kotlin Typecast Exception。我切换到在MacBook上拉的远程分支,但收到此错误:

09-23 12:28:29.112 4141-4141/com.example.pmarl.peedeehealthadvisor E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.pmarl.peedeehealthadvisor, PID: 4141
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pmarl.peedeehealthadvisor/com.example.pmarl.peedeehealthadvisor.MainActivity}: kotlin.TypeCastException: null cannot be cast to non-null type android.widget.Button
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access$800(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        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:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: kotlin.TypeCastException: null cannot be cast to non-null type android.widget.Button
        at com.example.pmarl.peedeehealthadvisor.MainActivity.onCreate(MainActivity.kt:31)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
        at android.app.ActivityThread.access$800(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5254) 
        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:903) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

我再次恐慌并测试了MacBook,但工作正常。

`

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
{
    private Button MyHealthData;
    private Button  MyHealthResources;
    private boolean firstStart;

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

        /*First time open code*/
        SharedPreferences app_preferences = getApplicationContext()
                .getSharedPreferences("MyPref",0);
        SharedPreferences.Editor editor = app_preferences.edit();

        firstStart = app_preferences.getBoolean("first_time_start",true);

        /*If statement to see if the app has been open before or not*/
        if(firstStart)
        {

            /*If the app hasn't been opened before, it runs the following code
            * and sets firstStart to false*/
            editor.putBoolean("first_time_start",false);
            editor.commit();

            //do your one time code here
            launchFirstTimeLogIn();
            //Toast.makeText(this,"This is first app run!", Toast.LENGTH_LONG).show();

        }
        else
        {
            //app open directly
            Toast.makeText(this, "Welcome!!!", Toast.LENGTH_LONG).show();
        }




        this.MyHealthData = (Button) findViewById(R.id.MyHealthData);

        this.MyHealthData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                launchMyHealthData();
            }
        });

        this.MyHealthResources = (Button) findViewById(R.id.HealthResources);



        this.MyHealthResources.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                launchSearchServiceActivity();
            }
        });

    }


    private void launchMyHealthData()
    {
        Intent intent = new Intent (this, MyHealthDataActivity.class);
        intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        finish();

    }

    private void launchSearchServiceActivity()
    {
        Intent intent = new Intent (this, SearchServiceActivity.class);
        intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        finish();

    }

    private void launchFirstTimeLogIn()
    {
        Intent intent = new Intent(this, FirstTimeLogin.class);
        intent.setFlags(intent.FLAG_ACTIVITY_NEW_TASK | intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
        finish();
    }
}

`

两者都是相同的Gradle版本和相同的Android Studio版本。我不确定这是什么原因?我的逻辑是说,如果是导致问题的Button,就像我发现的帖子所说的那样,它将无法在我的MacBook上运行。

谢谢大家的帮助!

1 个答案:

答案 0 :(得分:0)

  

null不能强制转换为非null类型的android.widget.Button

我假设您像这样声明了Button :(例如

mineBtn = findViewById(R.id.yourbtnid) as Button

?之后添加“ as”:

mineBtn = findViewById(R.id.yourbtnid) as? Button

那错误应该消失了。


编辑:在声明小部件之前删除this.。并声明一个这样的小部件:

MyHealthData = (Button) findViewById(R.id.MyHealthData);

请注意,最好从xml布局更改R.id.MyHealthData,以确保将来不会引起其他问题。