语法错误标记“;”,{预期

时间:2014-05-26 03:30:09

标签: java android

我正在尝试在android adt中构建一个简单的应用程序,只需在点击屏幕时选择5个选项中的一个并将其打印到屏幕上。我甚至不知道我的代码是否会起作用,因为我是java的新手,但到目前为止唯一的错误就是使用public static final。在此之前,它只是默认代码。

        /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }
    public static final String TAG = MainActivity.class.getSimpleName();
    if(GestureDetector.OnGestureListener){
        String[] myStringArray = {"Option 1","Option 2","Option 3", "Option 4", "Option 5"};
        Random rand = new Random();
        int  n = rand.nextInt(4) + 1;
        Log.d(TAG, myStringArray[n]);
        }
}}

2 个答案:

答案 0 :(得分:0)

  /**
 * A placeholder fragment containing a simple view.
 */

    public static class PlaceholderFragment extends Fragment {


     // place this line here........................      
 public static final String TAG = MainActivity.class.getSimpleName();

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        } }

    if(GestureDetector.OnGestureListener){
        String[] myStringArray = {"Option 1","Option 2","Option 3", "Option 4", "Option 5"};
        Random rand = new Random();
        int  n = rand.nextInt(4) + 1;
        Log.d(TAG, myStringArray[n]);
        } }}

答案 1 :(得分:0)

您错误地放置了if()

if(GestureDetector.OnGestureListener){
    String[] myStringArray = {"Option 1","Option 2","Option 3", "Option 4", "Option 5"};
    Random rand = new Random();
    int  n = rand.nextInt(4) + 1;
    Log.d(TAG, myStringArray[n]);
    }

上面的if()在你的代码中没有意义。这就是你得到的语法错误的原因。 你需要删除2个多余的大括号(}})