为什么我的烤面包应用程序无法编译?

huangapple go评论78阅读模式
英文:

Why is my toast application not compiling

问题

package com.example.toast;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import static android.widget.Toast.LENGTH_LONG;

public class MainActivity extends AppCompatActivity {

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

    public void fun(View v) {
        Toast t;
        t = Toast.makeText(this, "Welcome to Android", Toast.LENGTH_LONG);
        t.show();
    }
}

The words "context" and "resid" are corrected in the code snippet. The corrected code should resolve the issue you mentioned, and the error you were facing should be resolved.

英文:
    package com.example.toast;

    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Toast;

    import static android.widget.Toast.LENGTH_LONG;

    public class MainActivity extends AppCompatActivity {

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

     public void fun(View v) {
     Toast t;
     t = Toast.makeText( context:this, resid: "Welcome to Android", Toast.LENGTH_LONG);
     t.show();
    }

The words "conext" and "resid" are highlighted in red so I'm not sure what is going on. The code will not compile and I'm struggling to understand why. I am following the instructions word for word from the book.

I am getting this error:
AAPT: error: not well-formed (invalid token).

答案1

得分: 3

你是在观看视频还是图片并复制了代码?

context:resid:是IDE提示,不是方法调用中的实际文本。

英文:

Were you watching a video or image and copied the code?

The context: and resid: are IDE hints, not actual text in the method call.

答案2

得分: 1

仅需输入

     Toast.makeText(MainActivity.this, "欢迎来到安卓", Toast.LENGTH_LONG).show();

未显示的原因
你在输入"context:"和"resid",但实际上,你只需要这三个参数:

  1. 上下文(Context)= MainActivity.this(在你的情况下)
  2. 要显示的文本 = 你的自定义文本
  3. Toast的显示时长 = 根据情况选择 Toast.LENGTH_SHORT 或 Toast.LENGTH_LONG。
英文:

Just Type

 Toast.makeText( MainActivity.this, "Welcome to Android", Toast.LENGTH_LONG).show();

Reason For not showing
you were typing out "context:" and "resid" but in fact, you only need the 3 parameters

  1. Context = MainActivity.this in your case
  2. Test to show = your custom text
  3. Duration of Toast = Toast.LENGTH_SHORT/Toast.LENGTH_LONG depending upon your situation.

答案3

得分: 0

Just remove context: and resid: from the arguments. Those are syntax errors in Java, so the code won't compile.

    package com.example.toast;

    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Toast;

    import static android.widget.Toast.LENGTH_LONG;

    public class MainActivity extends AppCompatActivity {

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

      public void fun(View v) {
         Toast t;
         t = Toast.makeText(this, "Welcome to Android", Toast.LENGTH_LONG);
         t.show();
      }
    }
英文:

Just remove context: and resid: from the arguments. Those are syntax errors in Java, so the code won't compile.

package com.example.toast;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import static android.widget.Toast.LENGTH_LONG;

public class MainActivity extends AppCompatActivity {

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

  public void fun(View v) {
     Toast t;
     t = Toast.makeText(this, "Welcome to Android", Toast.LENGTH_LONG);
     t.show();
  }
}

huangapple
  • 本文由 发表于 2020年9月7日 01:55:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63767256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定