Toast.maketext错误

时间:2015-03-17 17:41:10

标签: android

编写Toast.LENGHT_LONG

后,代码下方出现红线

package com.example.neomn.myapplication1;

    import android.content.Context;
    import android.os.Bundle;
    import android.support.v7.app.ActionBarActivity;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;

公共类MainActivity扩展了ActionBarActivity {

        Button start = (Button) findViewById( R.id.btnstartservice );
        Button stop = (Button) findViewById( R.id.btnstopservice );


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

            start.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {



 Toast.makeText(this , "My First Service Started ",Toast.LENGTH_LONG).show();

                }
            });

4 个答案:

答案 0 :(得分:2)

因为,Toast.makeText()第一个参数需要应用程序上下文,并且在您的代码中,this依赖于Button的onClickListener类而不是Activity的上下文。

更改它
Toast.makeText(MainActivity.this , "My First Service Started ",Toast.LENGTH_LONG).show();

答案 1 :(得分:1)

您的Context," this"是错的。

更改:

 Toast.makeText(this, "My First Service Started ",Toast.LENGTH_LONG).show();

 Toast.makeText(MainActivity.this, "My First Service Started ",Toast.LENGTH_LONG).show();

以下是您的代码:

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {


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

        Button start = (Button) findViewById(R.id.btnstartservice);
        Button stop = (Button) findViewById(R.id.btnstopservice);


        start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(MainActivity.this, "My First Service Started ", Toast.LENGTH_LONG).show();

            }
        });
    }
}

在此处详细了解Contexthttp://developer.android.com/reference/android/content/Context.html

答案 2 :(得分:1)

写下以下行

Button start = (Button) findViewById( R.id.btnstartservice );
Button stop = (Button) findViewById( R.id.btnstopservice );

在您的OnCreate方法中,而不是在类级别编写它们,因为startstop会导致nullpointer,因为您在活动生命周期方法之外定义了那些。并且还会更改

 Toast.makeText(this , "My First Service Started ",Toast.LENGTH_LONG).show();

 Toast.makeText(MainActivity.this , "My First Service Started ",Toast.LENGTH_LONG).show();

即。将代码重写为

   import android.content.Context;
   import android.os.Bundle;
   import android.support.v7.app.ActionBarActivity;
   import android.view.Menu;
   import android.view.MenuItem;
   import android.view.View;
   import android.widget.Button;
   import android.widget.Toast;
   public class MainActivity extends ActionBarActivity {



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

        Button start = (Button) findViewById(R.id.btnstartservice);
        Button stop = (Button) findViewById(R.id.btnstopservice);


        start.setOnClickListener(new View.OnClickListener() {@Override
            public void onClick(View v) {

                Toast.makeText(MainActivity.this, "My First Service Started ", Toast.LENGTH_LONG).show();

            }
        });

答案 3 :(得分:1)

从被点击的视图中获取上下文(使用v参数)

@Override
public void onClick(View v) {
   Toast.makeText(v.getContext() , "My First Service Started ",Toast.LENGTH_LONG).show();
}