在令牌“}”上键入语法错误,删除此令牌

时间:2013-06-27 04:59:27

标签: java android

我在2个地方收到错误,我不知道为什么我是Android新手...

public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public boolean onTouchEvent(MotionEvent event)
 {
    Context context=getApplication();
    CharSequence text = "This is for ontouch event";
    int duration = Toast.LENGTH_SHORT;
     Toast Msg = Toast.makeText(context,text,duration);
     int x=(int)event.getX();
     int y=(int)event.getY();
     Msg.setGravity(Gravity.TOP|Gravity.LEFT, x, y);
     Msg.show();

     return true;
 } //Error type Syntax error on token "}", delete this token    

TextView t1=(TextView)findViewById(R.id.text);
t1.isClickable();

t1.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
        return true;
    }
});
}  // Error Syntax error, insert "}" to complete ClassBody

7 个答案:

答案 0 :(得分:2)

onCreate()方法中编写TextView代码,不能以任何函数编写此代码。

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

    TextView t1=(TextView)findViewById(R.id.text);
    t1.isClickable();

    t1.setOnLongClickListener(new View.OnLongClickListener() {
       @Override
       public boolean onLongClick(View v) {
          // TODO Auto-generated method stub
          Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
          return true;
       }
   });
}

答案 1 :(得分:1)

将其移至onCreate

  TextView t1=(TextView)findViewById(R.id.text);
  t1.setOnLongClickListener(new View.OnLongClickListener() {
  @Override
  public boolean onLongClick(View v) {
    // TODO Auto-generated method stub
    Toast.makeText(MainActivity.this ,"Long Clicked", Toast.LENGTH_SHORT).show();
    return true;
}
});

答案 2 :(得分:1)

粘贴整个代码并尝试此

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView t1=(TextView)findViewById(R.id.text);
   t1.isClickable();
   t1.setOnLongClickListener(new View.OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {
        Toast.makeText(getBaseContext(), "Text View Clicked",Toast.LENGTH_LONG).show();
        return false;
    }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;

}
@Override
public boolean onTouchEvent(MotionEvent event)
 {
    Context context=getApplication();
    CharSequence text = "This is for ontouch event";
    int duration = Toast.LENGTH_SHORT;
     Toast Msg = Toast.makeText(context,text,duration);
     int x=(int)event.getX();
     int y=(int)event.getY();
     Msg.setGravity(Gravity.TOP|Gravity.LEFT, x, y);
     Msg.show();

     return true;
  //Error type Syntax error on token "}", delete this token    


}  // 
}

这绝对有效并且长时间点击文本。确保它会给你输出。

答案 3 :(得分:0)

此代码不在任何函数中:

TextView t1=(TextView)findViewById(R.id.text);
    t1.isClickable();

    t1.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getBaseContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
            return true;
        }
    });

检查您的功能结构。

答案 4 :(得分:0)

代码的最后两段需要一个函数来包装它们,可能是onCreate。 您需要在视图上调用setLongClickable以进行长按以进行注册。

答案 5 :(得分:0)

试试这段代码......

班级档案......

package com.longclick;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

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

    TextView t1=(TextView)findViewById(R.id.text);
       t1.isClickable();

      t1.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
           // TODO Auto-generated method stub
           Toast.makeText(getApplicationContext(), "Long Clicked", Toast.LENGTH_SHORT).show();
           return true;
        }
     });

}

//@Override
//public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    //getMenuInflater().inflate(R.menu.activity_main, menu);
    //return true;
//}

@Override
public boolean onTouchEvent(MotionEvent event)
{
    Context context=getApplication();
    CharSequence text = "This is for ontouch event";
    int duration = Toast.LENGTH_SHORT;
     Toast Msg = Toast.makeText(context,text,duration);
     int x=(int)event.getX();
     int y=(int)event.getY();
     Msg.setGravity(Gravity.TOP|Gravity.LEFT, x, y);
     Msg.show();

    return true;
} //Error type Syntax error on token "}", delete this token    


}

.xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="@string/hello_world" />

</RelativeLayout>

我认为这对你有用......

enter image description here

demo here

答案 6 :(得分:0)

试试这个,

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView t1=(TextView)findViewById(R.id.textView1);
t1.isClickable();
t1.setOnLongClickListener(new View.OnLongClickListener() {

@Override
public boolean onLongClick(View v) {
    Toast.makeText(getBaseContext(), "Text View Clicked",Toast.LENGTH_LONG).show();
    return false;
}
});

}

长时间点击文字,不要快速离开。

工作正常。